Uploaded by Keyansh Vaish

MakeUPESC111

advertisement
ESC111A Makeup Endsem Exam
Feb 22, 2023
Name ..................................
Duration 2 hours
Time 12:30 - 14:30 hours
Roll no. ..................................
Max Points: 40
Closed Book and
Section .....................................
Closed Notes
Declaration
I solemnly declare that I have not used any unfair means during the exam.
Signature...................................
- DO NOT WRITE ANYTHING BELOW THIS LINE Ques 1
Ques 2
Ques 3
Ques 4
Marks
Graded By
1
TOTAL
Instructions
1. The exam is closed book and closed notes.
2. Use of mobile phones or access to internet is not allowed. Please do not keep your
mobile phone or any such device with you during the exam. These should be deposited
in safe visible corner of the room away from you before start of the exam and taken
back when the exam ends.
3. Anyone found to have mobile phone with him/her during the exam, even if not using
it, will be considered to have used unfair means.
4. The exam paper has 8 pages. There are a total of 4 questions. Each question starts
with a new page.
5. Write your name, roll no. and section on the front page as well as on other pages top
whenever a designated space for it is provided.
6. Write your nal answers neatly in the space provided, strike out any rough work.
7. Space for rough work is provided at the bottom of some pages.
rough work below this line 2
Name ................................... Roll no. .................... Section .............
Q1(points 6x1+4)
(a)
After each of the following statements, write
T
or
F
depending on if they are True or
False.
(i)
Automatic variables are initialized to
(ii)
(iii)
Global variables are initialized to
0
0
by default.
by default.
A static variable may initially contain an arbitrary value, if not explicity
initialized.
(iv)
Declaration
int a[4] = {0,1,2,3,4};
compiles and assigns
(v)
a[i]
value
i,
for
0<=i<=3.
Declaration
char name[10] = "ESC111A"
sets
(vi)
name[i]
to
0,
for
7<=i<=9.
Code fragment
int a[]; a={0,1,2};
declares and initializes
(b)
a
as shown.
Write a string literal to represent string
abc"%d"1\23
(to clarify, fourth character of
the given string is a double quote.)
rough work below this line 3
Name ................................... Roll no. .................... Section .............
Q2(points 4)
Suppose we have a string constructed only from character symbols, `('
and `)'. We call such a string balanced, if parentheses are properly nested and each
left (right) parenthesis has a corresponding matching right (left) parenthesis.
For
example, string (())() is balanced. Strings, ()), and (() on the other hand
are not balanced. Note that parentheses can be arbitrarily deeply nested.
Function
and
0
balanced
below takes as input a string
s
and returns
1
if it is balanced
otherwise. In code of this function below, some parts are missing, you need to
supply them.
int balanced(char s[]){
int c = ...1...;
for(int i=0; s[i] != `\0'; i++)
switch(s[i]){
case `(': ...2...; break;
case `)': if(c==0)return 0;
else ...3...;
break;
default : printf("improper input\n");
return 0;
}
return (...4...);
}
Write below in your answers to parts
...i...
(i),
simple expressions that if lled in spaces
in the code above then it works as expected.
(1)
(2)
(3)
(4)
4
Name ................................... Roll no. .................... Section .............
Q3(points 10)
Function call
string in
s.
intToSt takes an integer
intToSt(d,s), is expected to store
The function
and a character array argument.
decimal representation of
d
as a
Code of this function with some missing parts is shown below.
/* Call intToSt(d,s) with values of d shown
results in setting s as shown.
Examples: d=234 s="234"
d=0 s="0"
d=-12 s="-12" */
void intToSt(int d, char s[]){
if(d==0){s[0]=...1...;
s[1]=...2...; return;}
int sign=0;
if (d<0) {sign = 1; d =...3...;}
int i=0;
for( ;d>0;d=...4...,i++){
s[i]=...5...+ d % 10; }
if(sign)s[i++]=...6...;
...7...=0;
int j=0;
int temp;
while(j<i){
temp = s[i]; s[i]= s[j]; s[j] = temp;
i--; j++;
}
}
rough work below this line 5
Name ................................... Roll no. .................... Section .............
(Q3 Continued)
in spaces
(1)
Write below in your answers to parts
...i...
(i),
simple expressions that if lled
in the code above then it works as expected.
(2)
(3)
(4)
(5)
(6)
(7)
rough work below this line 6
Name ................................... Roll no. .................... Section .............
Q4(points 12+2+2)
In this question we consider a function called
partition
shown
partition(a,i,j), let x=a[i]. This call, rearranges elements of a
and returns a k, i<=k<=j, s.t. after the call array elements a[i...k-1] are <= x,
a[k]=x and array elements a[k+1...j] are > x. Note that ranges, a[i...k-1] and
a[k+1...j] may be empty also.
below.
In call
In code of the function
partition
to supply the missing parts.
shown below some parts are missing, you need
Function
partition
description is given, its code is omitted and is
not
also uses a function
swap
whose
to be supplied.
/* swap a[i] and a[j] */
void swap(int a[], int i, int j);
/* Assumes i<=j.
Let x=a[i], where a is the input array.
Rearranges elements of a and returns k, i<=k<=j s.t.
a[i...k-1] <=x, a[k]==x and a[k+1...j] >x. */
int partition(int a[], int i, int j){
if(i=j)return i;
int x = a[i];
int k=i+1, p=j;
while(...1...)
if(a[k]<=x)k++;
else {swap(a,...2...,...3...); ...4... ;}
if(a[k]<=x){swap(a,...5...,...6...); return k;}
else {swap(a,...7..., ...8...); return ...9...;}
}
rough work below this line 7
Name ................................. Roll no. .................... Section .............
(Q4 Continued)
(a)
Write below in your answers to parts
...i...
(b)
(i),
simple expressions that if lled in spaces
in the code above then it works as expected.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
In call
partition(a, 3, 9),
where
a = {0,0,0,-1,1,-3,3,2,2,7,0},
what is the
value returned by partition and what are the array contents after the call?
(c)
In call
partition(a, 0, 4),
where
a = {1,2,3,4,5},
what is the value returned by
partition and what are the array contents after the call?
rough work below this line 8
Download