Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) (→Data types) |
||
Line 1,128: | Line 1,128: | ||
+abcpee | +abcpee | ||
− | + | <br> | |
-abc<br>pee | -abc<br>pee | ||
− | + | <br> | |
-Compile error | -Compile error | ||
− | + | <br> | |
-abc<br>ee | -abc<br>ee | ||
Line 1,196: | Line 1,196: | ||
+ 8 1 <br> 12 1 | + 8 1 <br> 12 1 | ||
− | + | <br> | |
- 12 1 <br> 12 1 | - 12 1 <br> 12 1 | ||
− | + | <br> | |
-Compile error | -Compile error | ||
− | + | <br> | |
-11 1 <br> 12 1 | -11 1 <br> 12 1 | ||
||p is a char pointer and all pointers on 64 bit platform takes 64bits -> 8 bytes <br>*p is a char and size of char is 1 byte <br>q is an array and sizeof array is the no. of elements in the array X sizeof an element<br>= 12 (including \0 which is added by compiler at the end of all string literals) * 1 = 12 bytes | ||p is a char pointer and all pointers on 64 bit platform takes 64bits -> 8 bytes <br>*p is a char and size of char is 1 byte <br>q is an array and sizeof array is the no. of elements in the array X sizeof an element<br>= 12 (including \0 which is added by compiler at the end of all string literals) * 1 = 12 bytes |
<quiz display="simple"> {What is the output of this program? <syntaxhighlight lang="c" >
int main() {
char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); return 0;
} </syntaxhighlight> |type="()" /} -(a) How are you?
+(b) are you?
-(c) are
-(d) No output
{ Which of the following will print the value 2 for the above code? <syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; a[5][2][1] = 2;
return 0;
} </syntaxhighlight> |type="()" /} -(a) printf("%d",*(((a+5)+2)+1));
-(b) printf("%d",***((a+5)+2)+1);
+(c) printf("%d",*(*(*(a+5)+2)+1));
-(d) None of these
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; int b = ++a * a++; printf("%d ",b); return 0;
} </syntaxhighlight> |type="()" /} -(a)25
-(b) 30
-(c) 36
+(d) Undefined Behavior
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> |type="()" /} +(a) 5
-(b) 4
-(c) 3
-(d) None of these
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2,b = 5; a = a^b; b = b^a; printf("%d %d",a,b);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5 2
-(b) 2 5
-(c) 7 7
+(d) 7 2
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a[][3] = {1, 2, 3, 4, 5, 6}; int (*ptr)[3] = a; printf("%d %d ", (*ptr)[1], (*ptr)[2]); ++ptr; printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
return 0;
} </syntaxhighlight> |type="()" /} +(a) 2 3 5 6
-(b) 2 3 4 5
-(c) 4 5 0 0
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
void f(char**);
int main() {
char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv);
return 0;
} void f(char **p) {
char *t; t = (p += sizeof(int))[-1]; printf("%s\n", t);
} </syntaxhighlight> |type="()" /} -(a) ab
-(b) cd
-(c) ef
+(d) gh
{What is the output of the following program?
<syntaxhighlight lang="c">
int ripple(int n, ...) {
int i, j, k; va_list p; k = 0; j = 0; va_start(p, n); for (; j < n; ++j) { i = va_arg(p, int); k += i; } va_end(p); return k;
} int main() {
printf("%d\n", ripple(2, 5, 7));
return 0;
} </syntaxhighlight> |type="()" /} +(a) 12
-(b) 5
-(c) 7
-(d) 15
{What is the output of the following program?
<syntaxhighlight lang="c">
int counter(int i) {
static int count = 0; count = count + i; return count;
}
int main() {
int i, j; for (i = 0; i <= 5; i++) j = counter(i); printf("%d\n", j);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 10
+(b) 15
-(c) 6
-(d) 7
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5
-(b) 10
+(c) Compile Error
-(d) Garbage value
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i; i = x*x*x; printf("%d",i);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 125
+(b) 13
-(c) 17
-(d) None of above
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char c=125; c=c+10; printf("%d",c);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 135
-(b) +INF
+(c) -121
-(d) -8
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater"); else printf("Lesser");
return 0;
} </syntaxhighlight> |type="()" /} -(a) Equal
-(b) Greater
-(c) Lesser
+(d) Compile Error
{Consider the following code segment:
<syntaxhighlight lang="c">
int *f1() {
int x = 10; return &x;
} int *f2() {
int *ptr; *ptr = 10; return ptr;
} int *f3() {
int *ptr; ptr = (int*) malloc(sizeof (*ptr)); return ptr;
} </syntaxhighlight> Which of these functions uses pointers incorrectly? |type="()" /} +(a) f3 only
-(b) f1 and f3
-(c) f1 and f2
-(d) f1, f2, and f3
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i = 3; int j; j = sizeof(++i + ++i); printf("i=%d j=%d\n", i, j);
return 0;
} </syntaxhighlight> |type="()" /} -(a) i=4 j=4
+(b) i=3 j=4
-(c) i=5 j=4
-(d) the behavior is undefined
{ What is the output of the following program?
<syntaxhighlight lang="c">
void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);
int main() {
int a = 3; int b = 5; p[0] = f1; p[1] = f2; p[0](&a, b); printf("%d %d ", a, b); p[1](&a, b); printf("%d %d\n", a, b);
return 0;
}
void f1(int *p, int q) {
int tmp = *p; *p = q; q = tmp;
} void f2(int *p, int q) {
int tmp = *p; *p = q; q = tmp;
} </syntaxhighlight> |type="()" /} +(a) 5 5 5 5
-(b) 3 5 3 5
-(c) 5 3 3 5
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
void e(int);
int main() {
int a = 3; e(a); putchar('\n');
return 0;
} void e(int n) {
if (n > 0) { e(--n); printf("%d ", n); e(--n); }
} </syntaxhighlight> |type="()" /} +(a)0 1 2 0
-(b) 0 1 2 1
-(c) 1 2 0 1
-(d) 0 2 1 1
{ Consider the following code segment:
<syntaxhighlight lang="c">
typedef int (*test)(float*, float*);
test tmp;
</syntaxhighlight>
What is the type of tmp?
|type="()"
/} -(a) function taking two pointer-to-float arguments and returning pointer to int
-(b) pointer to int
+(c) pointer to function taking two pointer-to-float arguments and returning int
-(d) none of the above
{ What is the output of the following program?'
<syntaxhighlight lang="c">
int main() {
char p; char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; p = (buf + 1)[5]; printf("%d\n", p);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5
-(b) 6
+(c) 9
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
struct node { int a; int b; int c; }; struct node s = { 3, 5, 6 }; struct node *pt = &s; printf("%d\n", *((int*)pt+1));
return 0;
} </syntaxhighlight> |type="()" /} -(a) 3
+(b) 5
-(c) 6
-(d) 7
{What is the output of the following program?
<syntaxhighlight lang="c">
int main(void) {
char a[5] = { 1, 2, 3, 4, 5 }; char *ptr = (char*)(&a + 1); printf("%d %d\n", *(a + 1), *(ptr - 1));
return 0;
} </syntaxhighlight> |type="()" /} -(a) Compile Error
-(b) 2 1
+(c) 2 5
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
void foo(int[][3]);
int main(void) {
int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; foo(a); printf("%d\n", a[2][1]);
return 0;
}
void foo(int b[][3]) {
++b; b[1][1] = 9;
} </syntaxhighlight> |type="()" /} -(a) 8
+(b) 9
-(c) 7
-(d) none of the above
{Consider the following function:
<syntaxhighlight lang="c">
int foo(int x, int n)
{
int val = 1; if (n > 0) { if (n % 2 == 1) val *= x; val *= foo(x * x, n / 2); } return val;
} </syntaxhighlight>
What function of x and n is computed by foo? |type="()" /} +(a) $x^n$
-(b) $x×n$
-(c) $n^x$
-(d) none of the above
{What is the output of the following program? <syntaxhighlight lang="c">
int main() {
int a = 0; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5
-(b) 4
+(c) 3
-(d) 0
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2; if(a == (1,2)) printf("Hello"); if(a == 1,2) printf("World");
return 0;
} </syntaxhighlight> |type="()" /} -(a) Hello
-(b) World
+(c) HelloWorld
-(d) Compile Error
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 1,2; int b = (1,2); if(a == b) printf("Equal"); else printf("Not Equal");
return 0;
} </syntaxhighlight> |type="()" /} -(a) Equal
-(b) Not Equal
-(c) Compiler Dependent
+(d) Compile Error
{What is the output of the following program?
<syntaxhighlight lang="c">
void foo(char *);
int main() {
char *string = "Hello"; foo(string); printf("%s",string);
return 0;
}
void foo(char *a) {
while(*a) { *a += 1; a++; }
} </syntaxhighlight> |type="()" /} -(a) Hello
-(b) Ifmmp
-(c) Compile Error
+(d) Segmentation fault
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char s[] = "Hello World"; int i = 0; while(*(s++)) i++; printf("%d",i);
return 0;
} </syntaxhighlight> |type="()" /} -(a) Segmentation Fault
+(b) Compile Error
-(c) 12
-(d) 0
{What is the output of the following program?
<syntaxhighlight lang="c">
int a = 10; int main() {
fun(); fun();
return 0;
}
int fun() {
static int a = 1; printf("%d ",a); a++;
return 0;
} </syntaxhighlight> |type="()" /} +(a) 1 2
-(b) 1 1
-(c) 10 11
-(d) 10 10
{What is the output of the following program?
<syntaxhighlight lang="c">
int begin() {
printf("Hello\n");
return 0;
} </syntaxhighlight> |type="()" /} +(a) Hello
-(b) Link error
-(c) Segmentation fault
-(d) Compiler error
{Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a[10][20][30]={0}; printf("%ld",&a+1 - &a);
return 0;
} </syntaxhighlight> What is the output of this program? |type="{}" /} { 1 }
{Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; int *b = a; int *c = a+1; printf("%ld", c-b);
return 0;
} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings) |type="{}" /} { 600 }
{ What is the output of the following program? <syntaxhighlight lang="c">
int* fun();
int main() {
int *a = fun(); printf("%d",*a);
return 0;
} int* fun() {
int *a =(int*) malloc(sizeof(int)); *a = 10; return a;
} </syntaxhighlight> |type="{}" /} { 10 }
{ What is the output of the following program? <syntaxhighlight lang="c">
int main() {
int *a = fun(); printf("%d",a);
return 0;
} int fun() {
int a = 10; return a;
} </syntaxhighlight> |type="{}" /} { 10 }
{What is the output of the following program? <syntaxhighlight lang="c">
int main() {
char string[] = "Hello"; printf("%zu %zu",sizeof(string),strlen(string));
return 0;
} </syntaxhighlight> |type="{}" /} { 6 5 }
{What is the output of the following program? <syntaxhighlight lang="c">
int main() {
float a = 0.5; if(a == 0.5) printf("Yes"); else printf("No");
return 0;
} </syntaxhighlight> |type="{}" /} { Yes }
{What is the output of the following program? <syntaxhighlight lang="c">
void foo(char *);
int main() {
char a[100] = {0}; printf("%zu %zu",sizeof(a),strlen(a));
return 0;
} </syntaxhighlight> |type="{}" /} { 100 0 }
{What is the output of the following program? (Assume input is 10)
<syntaxhighlight lang="c">
int main() {
int a; printf("%d",scanf("%d",&a));
return 0;
} </syntaxhighlight> |type="{}" /} { 1 }
{If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000
0000 0000 0000, what will be the output of the following program?
<syntaxhighlight lang="c">
int main() {
float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i<2; i++) printf("%x", (unsigned char)(p[i]^p[3-i]));
return 0;
} </syntaxhighlight> |type="{}" /} { 40ac }
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char str[] = {'a','b','c','\0'}; str[0] -= 32; printf("%s",str);
return 0;
} </syntaxhighlight> |type="{}" /} { Abc }
</quiz>
<quiz display="simple"> {Consider an implementation where int is 4 bytes and long int is 8 bytes. Which of the following initializations are correct? <syntaxhighlight lang="c">
int main() {
long int a = 0x7fffffff * 0x7ffffff; long int b = 0x7ffffffff * 0x7ffffff; long int c = 0x7fffffff * 0x7fffffff; long int d = 0x7fffffff * 0x7fffffffl; printf("a = %ld, b = %ld, c = %ld, d = %ld\n", a, b, c, d); return 0;
}
</syntaxhighlight>
|type="()"
/}
-All are correct
-a, c, d
+b, d
-a, d
||a and c choices cause integer overflow. Even though long int can hold 8 bytes as given in the question, the operands are of 4 bytes only and hence the result is also 4 bytes.
In b choice, 0x7ffffffff is taking more than 4 bytes and hence is considered a long int. So, the next operand is implicitly typecasted to long int and the result is also calculated as long int. Hence there'll be no overflow.
In d choice, by adding l at the end, we force the compiler to use long int operand and hence the operations will be done using 8 byte operands and there will be no overflow.
{Consider an implementation where int is 4 bytes and long int is 8 bytes. What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
int i = 0; size_t a = sizeof i, b = sizeof (long); printf("a = %zd, b = %zd\n", a, b); //If %zd is given the compiler will automatically give it the correct type whether short, long or normal. This is useful for special data types like size_t whose size is implementation specific return 0;
} </syntaxhighlight> |type="()" /} -Compile error -Runtime error -a = 4, b = 4 +a = 4, b = 8 ||sizeof is an operator and hence we don't need a parentheses for giving variables to sizeof. So, sizeof i will return 4 which is the size of int and sizeof(long) will return 8
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
unsigned int a = 5; if(a > -1) printf("5 is > -1\n"); return 0;
} </syntaxhighlight> |type="()" /}
-5 is > -1
+No output
-Compile Error
-Runtime Error
||When an operation involves different data types the lower ranked one is promoted to higher ranked one. So, when we compare a signed int with an unsigned int, the signed int will be promoted to unsigned (unsigned has higher rank than signed).
a > -1
will turn to
00...101 > 11...111
and will evaluate to false
If a was declared as signed, then the if condition would have behaved as expected.
(Ideally an unsigned integer would never be compared with a negative number in real world and so do in programs)
{What will be printed by the following code? <syntaxhighlight lang="c">
int main() {
char buff[255] = "abc\
pee";
printf("%s", buff); return 0;
} </syntaxhighlight> |type="()" /}
+abcpee
-abc
pee
-Compile error
-abc
ee
||\ followed by a newline will make the compiler escape both the characters. So, this is used to write long lines of code across multiple lines for enhancing readability. So,
"abc\
pee";
is equivalent to
"abcpee";
{What will be printed by the following code? <syntaxhighlight lang="c">
int main() {
char buff[] = "abc" "hello"; printf("%zd\n", strlen(buff)); return 0;
} </syntaxhighlight> |type="()" /}
+8 -9 -10 -Compile Error ||Compiler will append any continuous string literals given inside "" and make a single string literal ignoring the space(s) between them. So, "abc" "hello" will become "abchello" and its length is 8
{How can you print the following sentence exactly as it is by changing the assignment to buff?
"Hello\\" "World\\"
<syntaxhighlight lang="c">
int main() {
char buff[255] = "\0"; printf("%s", buff); return 0;
} </syntaxhighlight> |type="()" /}
-char buff[255] = "\"Hello/\/\\" \"World/\/\\""; +char buff[255] = "\"Hello\\\\\" \"World\\\\\""; -char buff[255] = ""Hello\\" "World\\""; -char buff[255] = ""Hello\\\\\" "World\\\\""; ||To put " in a string we have to escape it with \. i.e., use \" instead of ". For '\' also we do the same \\ instead of \
{What will be the output of the following code? (Assume a 64 bit machine/compiler) <syntaxhighlight lang="c">
int main() {
char *p = "Hello World"; char q[] = "Hello World"; printf("%zd %zd", sizeof p, sizeof *p); printf("\n"); printf("%zd %zd", sizeof q, sizeof *q); return 0;
} </syntaxhighlight> |type="()" /}
+ 8 1
12 1
- 12 1
12 1
-Compile error
-11 1
12 1
||p is a char pointer and all pointers on 64 bit platform takes 64bits -> 8 bytes
*p is a char and size of char is 1 byte
q is an array and sizeof array is the no. of elements in the array X sizeof an element
= 12 (including \0 which is added by compiler at the end of all string literals) * 1 = 12 bytes
||*q is a char and sizeof char is 1 byte
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
{ char a = 5; int b = 5; if(a == b) printf("char and int compared equal\n"); } { int a = 5; long int b = 5;
if(a == b)
printf("int and long compared equal\n"); } { float a = 5.0; double b = 5.0; if(a == b) printf("float and double compared equal\n"); } { float a = 5.2; double b = 5.2; if(a == b) printf("float and double again compared equal\n"); } { float a = 5.2; if(a == 5.2) printf("float compared equal with constant\n"); } { double a = 5.2; if(a == 5.2) printf("double compared equal with constant\n"); } return 0;
}
</syntaxhighlight>
|type="()"
/}
+
char and int compared equal
int and long compare equal
float and double compared equal
double compared equal with constant
-
char and int compared equal
int and long compare equal
double compared equal with constant
-
char and int compared equal
int and long compare equal
float and double again compared equal
double compared equal with constant
-
char and int compared equal
int and long compare equal
float and double compared equal
|| When we compare an int and a char, the char will be promoted to int by adding 0s to the left. So, signed integers less than 128 (the maximum value representable by a signed char is 127) will compare equal with char.
||When we compare a long and an int, int is promoted to long by adding 0s to the left. So, any long number than can be represented by an int will compare equal with int.
||Like above , float will be promoted to double. If the float number is exactly representable (without any approximation) within a float, then it'll compare equal with its double counterpart.
||Here, 5.2 cannot be exactly representable using a float. Hence, its double version will have more accuracy and won't compare equal with float.
||By default all real values are taken as double. If we want to take as float we have to use
if(a == 5.2f)
instead of
if(a == 5.2)
</quiz>
1. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {
int sum = 0; while(n > 0) { n = n & n-1; sum++; } return sum;
} </syntaxhighlight> Ans:
2. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {
int c = a, d = b; while(a != b) { if(a < b) a = a+c; else b = b+d; } return a;
} </syntaxhighlight> Ans:
3. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {
int c = a-b; c = c&(0x80000000); return (!c)*a +(!!c)*b;
} </syntaxhighlight> Ans:
4. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {
int i; unsigned j = 0; for(i = 0; i < 32; i++) { j <<= 1; j += !!(a & 0x80000000); a <<= 1; if(j >=b) { j -= b; a++; } } return a;
} </syntaxhighlight> Ans:
5. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {
unsigned int i, x = 0, y = 0, z = 0; for(i = 0; i < 16; i++) { y <<= 2; y += !!(a & 0x80000000) << 1; y += !!(a & 0x40000000); a <<= 2; x = x + (x&1); x <<= 1; z <<= 1; if(x + 1 <= y) { x++; z++;
y-=x;
} } return z;
} </syntaxhighlight> Ans:
6. Write the code to dynamically allocate a 2-D array of size m x n.
Ans:
7. Declare a pointer to a function accepting an integer and returning void.
Ans:
8. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>. <syntaxhighlight lang="c">
int main() {
if(<condition>) {
printf("Hello ");
} else {
printf("World\n");
}
return 0;
} </syntaxhighlight> Ans:
9. Write a one line code to check if a number is a power of 2.
Ans:
10. Write a one line code to invert the last four bits of an integer.
Ans:
<quiz display="simple"> {What is the output of this program? <syntaxhighlight lang="c" >
int main() {
char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); return 0;
} </syntaxhighlight> |type="()" /} -(a) How are you?
+(b) are you?
-(c) are
-(d) No output
{ Which of the following will print the value 2 for the above code? <syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; a[5][2][1] = 2;
return 0;
} </syntaxhighlight> |type="()" /} -(a) printf("%d",*(((a+5)+2)+1));
-(b) printf("%d",***((a+5)+2)+1);
+(c) printf("%d",*(*(*(a+5)+2)+1));
-(d) None of these
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; int b = ++a * a++; printf("%d ",b); return 0;
} </syntaxhighlight> |type="()" /} -(a)25
-(b) 30
-(c) 36
+(d) Undefined Behavior
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 5; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> |type="()" /} +(a) 5
-(b) 4
-(c) 3
-(d) None of these
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2,b = 5; a = a^b; b = b^a; printf("%d %d",a,b);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5 2
-(b) 2 5
-(c) 7 7
+(d) 7 2
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a[][3] = {1, 2, 3, 4, 5, 6}; int (*ptr)[3] = a; printf("%d %d ", (*ptr)[1], (*ptr)[2]); ++ptr; printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
return 0;
} </syntaxhighlight> |type="()" /} +(a) 2 3 5 6
-(b) 2 3 4 5
-(c) 4 5 0 0
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
void f(char**);
int main() {
char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv);
return 0;
} void f(char **p) {
char *t; t = (p += sizeof(int))[-1]; printf("%s\n", t);
} </syntaxhighlight> |type="()" /} -(a) ab
-(b) cd
-(c) ef
+(d) gh
{What is the output of the following program?
<syntaxhighlight lang="c">
int ripple(int n, ...) {
int i, j, k; va_list p; k = 0; j = 0; va_start(p, n); for (; j < n; ++j) { i = va_arg(p, int); k += i; } va_end(p); return k;
} int main() {
printf("%d\n", ripple(2, 5, 7));
return 0;
} </syntaxhighlight> |type="()" /} +(a) 12
-(b) 5
-(c) 7
-(d) 15
{What is the output of the following program?
<syntaxhighlight lang="c">
int counter(int i) {
static int count = 0; count = count + i; return count;
}
int main() {
int i, j; for (i = 0; i <= 5; i++) j = counter(i); printf("%d\n", j);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 10
+(b) 15
-(c) 6
-(d) 7
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5
-(b) 10
+(c) Compile Error
-(d) Garbage value
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i; i = x*x*x; printf("%d",i);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 125
+(b) 13
-(c) 17
-(d) None of above
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char c=125; c=c+10; printf("%d",c);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 135
-(b) +INF
+(c) -121
-(d) -8
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater"); else printf("Lesser");
return 0;
} </syntaxhighlight> |type="()" /} -(a) Equal
-(b) Greater
-(c) Lesser
+(d) Compile Error
{Consider the following code segment:
<syntaxhighlight lang="c">
int *f1() {
int x = 10; return &x;
} int *f2() {
int *ptr; *ptr = 10; return ptr;
} int *f3() {
int *ptr; ptr = (int*) malloc(sizeof (*ptr)); return ptr;
} </syntaxhighlight> Which of these functions uses pointers incorrectly? |type="()" /} +(a) f3 only
-(b) f1 and f3
-(c) f1 and f2
-(d) f1, f2, and f3
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int i = 3; int j; j = sizeof(++i + ++i); printf("i=%d j=%d\n", i, j);
return 0;
} </syntaxhighlight> |type="()" /} -(a) i=4 j=4
+(b) i=3 j=4
-(c) i=5 j=4
-(d) the behavior is undefined
{ What is the output of the following program?
<syntaxhighlight lang="c">
void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);
int main() {
int a = 3; int b = 5; p[0] = f1; p[1] = f2; p[0](&a, b); printf("%d %d ", a, b); p[1](&a, b); printf("%d %d\n", a, b);
return 0;
}
void f1(int *p, int q) {
int tmp = *p; *p = q; q = tmp;
} void f2(int *p, int q) {
int tmp = *p; *p = q; q = tmp;
} </syntaxhighlight> |type="()" /} +(a) 5 5 5 5
-(b) 3 5 3 5
-(c) 5 3 3 5
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
void e(int);
int main() {
int a = 3; e(a); putchar('\n');
return 0;
} void e(int n) {
if (n > 0) { e(--n); printf("%d ", n); e(--n); }
} </syntaxhighlight> |type="()" /} +(a)0 1 2 0
-(b) 0 1 2 1
-(c) 1 2 0 1
-(d) 0 2 1 1
{ Consider the following code segment:
<syntaxhighlight lang="c">
typedef int (*test)(float*, float*);
test tmp;
</syntaxhighlight>
What is the type of tmp?
|type="()"
/} -(a) function taking two pointer-to-float arguments and returning pointer to int
-(b) pointer to int
+(c) pointer to function taking two pointer-to-float arguments and returning int
-(d) none of the above
{ What is the output of the following program?'
<syntaxhighlight lang="c">
int main() {
char p; char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; p = (buf + 1)[5]; printf("%d\n", p);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5
-(b) 6
+(c) 9
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
struct node { int a; int b; int c; }; struct node s = { 3, 5, 6 }; struct node *pt = &s; printf("%d\n", *((int*)pt+1));
return 0;
} </syntaxhighlight> |type="()" /} -(a) 3
+(b) 5
-(c) 6
-(d) 7
{What is the output of the following program?
<syntaxhighlight lang="c">
int main(void) {
char a[5] = { 1, 2, 3, 4, 5 }; char *ptr = (char*)(&a + 1); printf("%d %d\n", *(a + 1), *(ptr - 1));
return 0;
} </syntaxhighlight> |type="()" /} -(a) Compile Error
-(b) 2 1
+(c) 2 5
-(d) none of the above
{What is the output of the following program?
<syntaxhighlight lang="c">
void foo(int[][3]);
int main(void) {
int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; foo(a); printf("%d\n", a[2][1]);
return 0;
}
void foo(int b[][3]) {
++b; b[1][1] = 9;
} </syntaxhighlight> |type="()" /} -(a) 8
+(b) 9
-(c) 7
-(d) none of the above
{Consider the following function:
<syntaxhighlight lang="c">
int foo(int x, int n)
{
int val = 1; if (n > 0) { if (n % 2 == 1) val *= x; val *= foo(x * x, n / 2); } return val;
} </syntaxhighlight>
What function of x and n is computed by foo? |type="()" /} +(a) $x^n$
-(b) $x×n$
-(c) $n^x$
-(d) none of the above
{What is the output of the following program? <syntaxhighlight lang="c">
int main() {
int a = 0; switch(a) { default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a);
return 0;
} </syntaxhighlight> |type="()" /} -(a) 5
-(b) 4
+(c) 3
-(d) 0
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 2; if(a == (1,2)) printf("Hello"); if(a == 1,2) printf("World");
return 0;
} </syntaxhighlight> |type="()" /} -(a) Hello
-(b) World
+(c) HelloWorld
-(d) Compile Error
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
int a = 1,2; int b = (1,2); if(a == b) printf("Equal"); else printf("Not Equal");
return 0;
} </syntaxhighlight> |type="()" /} -(a) Equal
-(b) Not Equal
-(c) Compiler Dependent
+(d) Compile Error
{What is the output of the following program?
<syntaxhighlight lang="c">
void foo(char *);
int main() {
char *string = "Hello"; foo(string); printf("%s",string);
return 0;
}
void foo(char *a) {
while(*a) { *a += 1; a++; }
} </syntaxhighlight> |type="()" /} -(a) Hello
-(b) Ifmmp
-(c) Compile Error
+(d) Segmentation fault
{What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char s[] = "Hello World"; int i = 0; while(*(s++)) i++; printf("%d",i);
return 0;
} </syntaxhighlight> |type="()" /} -(a) Segmentation Fault
+(b) Compile Error
-(c) 12
-(d) 0
{What is the output of the following program?
<syntaxhighlight lang="c">
int a = 10; int main() {
fun(); fun();
return 0;
}
int fun() {
static int a = 1; printf("%d ",a); a++;
return 0;
} </syntaxhighlight> |type="()" /} +(a) 1 2
-(b) 1 1
-(c) 10 11
-(d) 10 10
{What is the output of the following program?
<syntaxhighlight lang="c">
int begin() {
printf("Hello\n");
return 0;
} </syntaxhighlight> |type="()" /} +(a) Hello
-(b) Link error
-(c) Segmentation fault
-(d) Compiler error
{Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a[10][20][30]={0}; printf("%ld",&a+1 - &a);
return 0;
} </syntaxhighlight> What is the output of this program? |type="{}" /} { 1 }
{Consider the following program:
<syntaxhighlight lang="c">
int main() {
int a[10][20][30] = {0}; int *b = a; int *c = a+1; printf("%ld", c-b);
return 0;
} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings) |type="{}" /} { 600 }
{ What is the output of the following program? <syntaxhighlight lang="c">
int* fun();
int main() {
int *a = fun(); printf("%d",*a);
return 0;
} int* fun() {
int *a =(int*) malloc(sizeof(int)); *a = 10; return a;
} </syntaxhighlight> |type="{}" /} { 10 }
{ What is the output of the following program? <syntaxhighlight lang="c">
int main() {
int *a = fun(); printf("%d",a);
return 0;
} int fun() {
int a = 10; return a;
} </syntaxhighlight> |type="{}" /} { 10 }
{What is the output of the following program? <syntaxhighlight lang="c">
int main() {
char string[] = "Hello"; printf("%zu %zu",sizeof(string),strlen(string));
return 0;
} </syntaxhighlight> |type="{}" /} { 6 5 }
{What is the output of the following program? <syntaxhighlight lang="c">
int main() {
float a = 0.5; if(a == 0.5) printf("Yes"); else printf("No");
return 0;
} </syntaxhighlight> |type="{}" /} { Yes }
{What is the output of the following program? <syntaxhighlight lang="c">
void foo(char *);
int main() {
char a[100] = {0}; printf("%zu %zu",sizeof(a),strlen(a));
return 0;
} </syntaxhighlight> |type="{}" /} { 100 0 }
{What is the output of the following program? (Assume input is 10)
<syntaxhighlight lang="c">
int main() {
int a; printf("%d",scanf("%d",&a));
return 0;
} </syntaxhighlight> |type="{}" /} { 1 }
{If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000
0000 0000 0000, what will be the output of the following program?
<syntaxhighlight lang="c">
int main() {
float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i<2; i++) printf("%x", (unsigned char)(p[i]^p[3-i]));
return 0;
} </syntaxhighlight> |type="{}" /} { 40ac }
{ What is the output of the following program?
<syntaxhighlight lang="c">
int main() {
char str[] = {'a','b','c','\0'}; str[0] -= 32; printf("%s",str);
return 0;
} </syntaxhighlight> |type="{}" /} { Abc }
</quiz>
<quiz display="simple"> {Consider an implementation where int is 4 bytes and long int is 8 bytes. Which of the following initializations are correct? <syntaxhighlight lang="c">
int main() {
long int a = 0x7fffffff * 0x7ffffff; long int b = 0x7ffffffff * 0x7ffffff; long int c = 0x7fffffff * 0x7fffffff; long int d = 0x7fffffff * 0x7fffffffl; printf("a = %ld, b = %ld, c = %ld, d = %ld\n", a, b, c, d); return 0;
}
</syntaxhighlight>
|type="()"
/}
-All are correct
-a, c, d
+b, d
-a, d
||a and c choices cause integer overflow. Even though long int can hold 8 bytes as given in the question, the operands are of 4 bytes only and hence the result is also 4 bytes.
In b choice, 0x7ffffffff is taking more than 4 bytes and hence is considered a long int. So, the next operand is implicitly typecasted to long int and the result is also calculated as long int. Hence there'll be no overflow.
In d choice, by adding l at the end, we force the compiler to use long int operand and hence the operations will be done using 8 byte operands and there will be no overflow.
{Consider an implementation where int is 4 bytes and long int is 8 bytes. What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
int i = 0; size_t a = sizeof i, b = sizeof (long); printf("a = %zd, b = %zd\n", a, b); //If %zd is given the compiler will automatically give it the correct type whether short, long or normal. This is useful for special data types like size_t whose size is implementation specific return 0;
} </syntaxhighlight> |type="()" /} -Compile error -Runtime error -a = 4, b = 4 +a = 4, b = 8 ||sizeof is an operator and hence we don't need a parentheses for giving variables to sizeof. So, sizeof i will return 4 which is the size of int and sizeof(long) will return 8
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
unsigned int a = 5; if(a > -1) printf("5 is > -1\n"); return 0;
} </syntaxhighlight> |type="()" /}
-5 is > -1
+No output
-Compile Error
-Runtime Error
||When an operation involves different data types the lower ranked one is promoted to higher ranked one. So, when we compare a signed int with an unsigned int, the signed int will be promoted to unsigned (unsigned has higher rank than signed).
a > -1
will turn to
00...101 > 11...111
and will evaluate to false
If a was declared as signed, then the if condition would have behaved as expected.
(Ideally an unsigned integer would never be compared with a negative number in real world and so do in programs)
{What will be printed by the following code? <syntaxhighlight lang="c">
int main() {
char buff[255] = "abc\
pee";
printf("%s", buff); return 0;
} </syntaxhighlight> |type="()" /}
+abcpee
-abc
pee
-Compile error
-abc
ee
||\ followed by a newline will make the compiler escape both the characters. So, this is used to write long lines of code across multiple lines for enhancing readability. So,
"abc\
pee";
is equivalent to
"abcpee";
{What will be printed by the following code? <syntaxhighlight lang="c">
int main() {
char buff[] = "abc" "hello"; printf("%zd\n", strlen(buff)); return 0;
} </syntaxhighlight> |type="()" /}
+8 -9 -10 -Compile Error ||Compiler will append any continuous string literals given inside "" and make a single string literal ignoring the space(s) between them. So, "abc" "hello" will become "abchello" and its length is 8
{How can you print the following sentence exactly as it is by changing the assignment to buff?
"Hello\\" "World\\"
<syntaxhighlight lang="c">
int main() {
char buff[255] = "\0"; printf("%s", buff); return 0;
} </syntaxhighlight> |type="()" /}
-char buff[255] = "\"Hello/\/\\" \"World/\/\\""; +char buff[255] = "\"Hello\\\\\" \"World\\\\\""; -char buff[255] = ""Hello\\" "World\\""; -char buff[255] = ""Hello\\\\\" "World\\\\""; ||To put " in a string we have to escape it with \. i.e., use \" instead of ". For '\' also we do the same \\ instead of \
{What will be the output of the following code? (Assume a 64 bit machine/compiler) <syntaxhighlight lang="c">
int main() {
char *p = "Hello World"; char q[] = "Hello World"; printf("%zd %zd", sizeof p, sizeof *p); printf("\n"); printf("%zd %zd", sizeof q, sizeof *q); return 0;
} </syntaxhighlight> |type="()" /}
+ 8 1
12 1
- 12 1
12 1
-Compile error
-11 1
12 1
||p is a char pointer and all pointers on 64 bit platform takes 64bits -> 8 bytes
*p is a char and size of char is 1 byte
q is an array and sizeof array is the no. of elements in the array X sizeof an element
= 12 (including \0 which is added by compiler at the end of all string literals) * 1 = 12 bytes
||*q is a char and sizeof char is 1 byte
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
{ char a = 5; int b = 5; if(a == b) printf("char and int compared equal\n"); } { int a = 5; long int b = 5;
if(a == b)
printf("int and long compared equal\n"); } { float a = 5.0; double b = 5.0; if(a == b) printf("float and double compared equal\n"); } { float a = 5.2; double b = 5.2; if(a == b) printf("float and double again compared equal\n"); } { float a = 5.2; if(a == 5.2) printf("float compared equal with constant\n"); } { double a = 5.2; if(a == 5.2) printf("double compared equal with constant\n"); } return 0;
}
</syntaxhighlight>
|type="()"
/}
+
char and int compared equal
int and long compare equal
float and double compared equal
double compared equal with constant
-
char and int compared equal
int and long compare equal
double compared equal with constant
-
char and int compared equal
int and long compare equal
float and double again compared equal
double compared equal with constant
-
char and int compared equal
int and long compare equal
float and double compared equal
|| When we compare an int and a char, the char will be promoted to int by adding 0s to the left. So, signed integers less than 128 (the maximum value representable by a signed char is 127) will compare equal with char.
||When we compare a long and an int, int is promoted to long by adding 0s to the left. So, any long number than can be represented by an int will compare equal with int.
||Like above , float will be promoted to double. If the float number is exactly representable (without any approximation) within a float, then it'll compare equal with its double counterpart.
||Here, 5.2 cannot be exactly representable using a float. Hence, its double version will have more accuracy and won't compare equal with float.
||By default all real values are taken as double. If we want to take as float we have to use
if(a == 5.2f)
instead of
if(a == 5.2)
</quiz>
1. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {
int sum = 0; while(n > 0) { n = n & n-1; sum++; } return sum;
} </syntaxhighlight> Ans:
2. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {
int c = a, d = b; while(a != b) { if(a < b) a = a+c; else b = b+d; } return a;
} </syntaxhighlight> Ans:
3. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {
int c = a-b; c = c&(0x80000000); return (!c)*a +(!!c)*b;
} </syntaxhighlight> Ans:
4. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {
int i; unsigned j = 0; for(i = 0; i < 32; i++) { j <<= 1; j += !!(a & 0x80000000); a <<= 1; if(j >=b) { j -= b; a++; } } return a;
} </syntaxhighlight> Ans:
5. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {
unsigned int i, x = 0, y = 0, z = 0; for(i = 0; i < 16; i++) { y <<= 2; y += !!(a & 0x80000000) << 1; y += !!(a & 0x40000000); a <<= 2; x = x + (x&1); x <<= 1; z <<= 1; if(x + 1 <= y) { x++; z++;
y-=x;
} } return z;
} </syntaxhighlight> Ans:
6. Write the code to dynamically allocate a 2-D array of size m x n.
Ans:
7. Declare a pointer to a function accepting an integer and returning void.
Ans:
8. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>. <syntaxhighlight lang="c">
int main() {
if(<condition>) {
printf("Hello ");
} else {
printf("World\n");
}
return 0;
} </syntaxhighlight> Ans:
9. Write a one line code to check if a number is a power of 2.
Ans:
10. Write a one line code to invert the last four bits of an integer.
Ans: