You are on page 1of 4

ESC112A Quiz-2

Name ..................................
Jan 10, 2023
Duration 30 Minutes
Max Marks: 15 Roll no. ..................................

Closed Book and


Closed Notes Section .....................................

Instructions

1. The exam is closed book and closed notes. Use of mobile phones or access to
internet is not allowed.

2. This quiz paper has two sheets or four pages. There are a total of three questions.

3. Write your name, roll no. and section on both this and the third page top, in the
spaces provided.

4. Write your nal answers neatly in the space provided, strike out any rough work.
Space for rough work is provided at the end of the question paper.

Q1 (points 5) Let p be of type int*. Write T or F after each of the following statement
depending on if it is true or false.

1 Expression *(p-1)++ does not give compilation error.


2 Expression (*(p-1))++ does not give compilation error.
3 Expressions (*p)- - and *p- - evaluate to the same value.
4. Expressions (*p)- - and *p- - are equivalent.
5. Expression &(p++) gives compilation error.
Q2 (points 5) In the code below, swapIntPt function is expected to swap values of two
variables of type int*. Function main uses swapIntPt to swap values of p,q. You
need to answer, what should be lled in the blanks so that main prints output
1,1
1,1

#include <stdio.h>
/* swaps values of two variables of type (int *) */
void swapIntPt(int ___1___, int ___2___v){
int ___3___ = *u;
*u = *v;
*v = temp;
}

int main(){
int x=5, y=10;
int *p=&x, *q=&y;
printf("%d,%d\n", (p==&x), (q==&y));
swapIntPt(___4___ , ___5___);
printf("%d,%d\n", (p==&y), (q==&x));
}

In answers to parts (n) below, write the expression that should be lled in places
___n___ so that the program works as described above.

(1) (4)

(2) (5)

(3)

2
Name ............................ Roll no. ................. Section .........

Q3 (points 5) One can write string functions using pointers instead of array index
notation. In such code, one typically uses a pointer to traverse through characters of
a string. Following are two examples of such codes. To understand them you need
to use the fact that int code of character `\0' is 0.

(a)

int f(char *s){


int i=1;
while(*s++);
return i;
}

What does f("abc") return?

(b) Following code is supposed to concatenate string t at the end of string s.


Assume that there is enough memory alloated to s so that it can hold the
concatenated string.
/* concatenates string t at the end of string s. */
void strcat(char *s, char *t){
while(*s++);
___1___;
while(*s++ = *t++);
}

What should be lled in ___1___ so that strcat works as expected?

3
Rough Work 

You might also like