1.
What will be the Output of the following program ? void main(){ const char var='A'; ++var; printf("%c",var);}
Correct Answer
C. ERROR
Explanation
The program will result in an error because the variable "var" is declared as a constant using the "const" keyword. Constants cannot be modified, so the attempt to increment "var" using the "++" operator will cause a compilation error.
2.
What will be the Output of the Following #include void main(){ int x=(20 || 40 ) && (10); printf("x= %d",x);}
Correct Answer
A. X= 1
Explanation
1)(20 || 40) .... both are non zero values, will return 1.
2)(1) && 10 .... both are non zero values, hence output will be 1
3.
What will be the Output of the program ?#include <stdio.h>void main(){ int intVar=20,x; x= ++intVar,intVar++,++intVar; printf("Value of intVar=%d, x=%d",intVar,x);}
Correct Answer
A. Value of intVar=23, x=21
Explanation
The program initializes the variable "intVar" with a value of 20. Then, the expression "x= ++intVar,intVar++,++intVar" is evaluated. The comma operator evaluates each expression from left to right and the result of the entire expression is the value of the rightmost expression.
First, "++intVar" increments the value of "intVar" to 21. Then, "intVar++" increments the value of "intVar" to 22, but the result of this expression is still 21. Finally, "++intVar" increments the value of "intVar" to 23.
Therefore, the value of "intVar" is 23 and the value of "x" is 21.
4.
What value of c will get printedmain(){ int a,b,c; a=10; b=20; c=printf("%d",a)+ ++b; printf("%d",c);}
Correct Answer
A. 23
Explanation
printf() will return no. of bytes it printed
Expression becomes
c = 2 + ++b;
then value of b is incremented before addition
5.
What will be the output of the program ?#includeint main(){ int i=3, *j, k; j = &i; printf("%d\n", i**j*i+*j); return 0;}
Correct Answer
A. 30
Explanation
The program initializes the variable i with a value of 3. It then declares a pointer variable j and assigns the address of i to j. The printf statement calculates the value of i multiplied by the value pointed to by j (which is the value of i itself) multiplied by i again, and adds the value pointed to by j (which is again the value of i). This results in the calculation 3 * 3 * 3 + 3, which equals 30. Therefore, the output of the program would be 30.
6.
What will be the output of following program ?#include <stdio.h>int main(){ int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); return 0;}
Correct Answer
C. 0 0 1 3 1
Explanation
In the given program, the variables i, j, k, l are initialized with -1, -1, 0, 2 respectively.
The expression i++&&j++&&k++||l++ is evaluated from left to right.
Since i is -1, it is first incremented to 0 and then checked. Since it is not 0, the evaluation continues.
Next, j is also incremented from -1 to 0, but since it is still 0, the evaluation stops and the result is 0.
Therefore, i and j remain 0, k is incremented to 1, l is incremented to 3, and m is assigned the value 1.
The printf statement then prints the values of i, j, k, l, and m which are 0, 0, 1, 3, and 1 respectively.
7.
What will be the output of the program ?#includeint main(){ char str[20] = "Hello"; char *const p=str; *p='M'; printf("%s\n", str); return 0;}
Correct Answer
A. Mello
Explanation
The program will output "Mello". The variable `str` is declared as a character array and initialized with the string "Hello". The pointer `p` is declared as a constant pointer to a character and assigned the address of `str`. Since `p` is a constant pointer, it cannot be reassigned to point to a different memory location. However, the value at the memory location it points to can be modified. In this case, `*p = 'M'` changes the first character of the string from 'H' to 'M'. Therefore, when `str` is printed using `printf`, it will display "Mello".
8.
What is the output of following program? void abc(int a, int b){ printf("%d%d",++a,++b); }void main(){ int a=10; abc(++a,a++); abc(a++,++a); printf("%d",a);}
Correct Answer
A. 13 11 14 14 14
Explanation
Compiler dependent- Use DEV C compiler.
9.
What will be the output of following program ? #include <stdio.h>int main(){ int MAX=10; int array[MAX]; printf("size of array is = %d",sizeof(array)); return 0;}
Correct Answer
B. Size of array is = 40
Explanation
The size of the array is determined by multiplying the number of elements in the array by the size of each element. In this case, the array has 10 elements and each element is an integer, which typically has a size of 4 bytes. Therefore, the size of the array is 10 * 4 = 40 bytes.
10.
What will be the output of the program ?#includeint main(){ char *str; str = "%s"; printf(str, "K\n"); return 0;}
Correct Answer
C. K
Explanation
The program will output "K". The variable "str" is assigned the value "%s", which is a format specifier for a string. When the printf function is called, it uses the value of "str" as the format string and "K" as the argument to be printed. Therefore, "K" is printed as the output.
11.
What will be the output of the program ?#includeint *check(static int, static int);int main(){ int *c; c = check(10, 20); printf("%d\n", c); return 0;}int *check(static int i, static int j){ int *p, *q; p = &i; q = &j; if(i >= 45) return (p); else return (q);}
Correct Answer
D. Error:cannot use static for function parameters
Explanation
The program will output "Error: cannot use static for function parameters." This is because the function "check" has two parameters declared as "static int," which is not allowed in C programming. The keyword "static" is used to declare variables with static storage duration, but it cannot be used for function parameters. Hence, the program will result in a compilation error.
12.
What will be the Output of the following #include <stdio.h>int main(){ char X[10]={'A'},i; for(i=0; i<10; i++) printf("%d ",X[i]); return 0;}
Correct Answer
B. 65 0 0 0 0 0 0 0 0 0
Explanation
The given code initializes an array X with only one element 'A' and the rest of the elements are not initialized. The loop runs for 10 iterations and tries to print the elements of array X. Since the rest of the elements are not initialized, they will have garbage values. The ASCII value of 'A' is 65, so the first element of X will be printed as 65 and the rest of the elements will be printed as 0 due to garbage values. Therefore, the output will be "65 0 0 0 0 0 0 0 0 0".
13.
What will be the output of the program if the size of pointer is 4-bytes?#includeint main(){ printf("%d, %d\n", sizeof(NULL), sizeof("")); return 0;}
Correct Answer
C. 4,1
Explanation
The output of the program will be "4,1". The sizeof(NULL) will return the size of a pointer, which is 4 bytes in this case. The sizeof("") will return the size of an empty string, which is 1 byte.
14.
What is the output of following program? void f1(){ static int s=5; ++s; printf("%d",s);} main(){ f1(); f1(); printf("%d",s);}
Correct Answer
A. Error
Explanation
s in out of scope in main()
15.
What will be printed as the result of the operation below:main() { int x=5; printf("%d,%d,%d",x,x<<2,x>>2);}
Correct Answer
A. 5,20,1
Explanation
The code snippet is a C program that uses the printf function to print the values of three variables: x, x2.
In the given program, the value of x is initialized to 5.
In the printf statement, the first argument is "%d,%d,%d", which specifies the format for printing three integers.
The second argument, x2, is a bitwise right shift operation on x. This shifts the binary representation of x two places to the right, resulting in 1.
Therefore, the program will print "5,20,1" as the output.
16.
What will be the output of the following ?#include void main(){char cnt=0;for(;cnt++;printf("%d",cnt)) ;printf("%d",cnt);}
Correct Answer
A. 1
Explanation
The program will output "1".
In the for loop, the condition is always true because the increment operation is executed after the condition is checked. So, the loop will keep running indefinitely.
Inside the loop, the printf statement will print the value of "cnt" which starts at 0 and gets incremented by 1 in each iteration. So, it will print "0" first, then "1", and so on.
However, since the loop is infinite, the program will not reach the second printf statement, and only "1" will be printed.
17.
What is the output of following program? void main(){ int a,b,c,d; a=b=c=d=1; a=++b>1 || ++c>1 && ++d>1; printf("%d%d%d%d",a,b,c,d);}
Correct Answer
A. 1211
Explanation
The program initializes variables a, b, c, and d to 1. Then, it evaluates the expression `++b>1 || ++c>1 && ++d>1`.
Since the `++b>1` is true (b becomes 2), the first part of the expression is true and the second part (`++c>1 && ++d>1`) is not evaluated.
Therefore, the value of a remains 1, b becomes 2, and c and d remain 1.
The printf statement prints the values of a, b, c, and d, which are 1211.
18.
What will be the output of the following .. ?#include <stdio.h>#include <string.h> int main(){char str[];strcpy(str,"Hello");printf("%s",str);return 0;}
Correct Answer
A. Error
Explanation
array size missing in 'str' while declaration
19.
: what is the output of following program? void main(){ printf("%d%d",47%5,47%-5); printf("%d%d%d",-47%5,-47%-5,5%7);}
Correct Answer
A. 2,2,-2,-2,5
Explanation
The program uses the printf function to print the results of various arithmetic operations.
In the first printf statement, the expression 47%5 calculates the remainder when 47 is divided by 5, which is 2. The expression 47%-5 calculates the remainder when 47 is divided by -5, which is also 2. Therefore, the first part of the output is 2,2.
In the second printf statement, the expression -47%5 calculates the remainder when -47 is divided by 5, which is -2. The expression -47%-5 calculates the remainder when -47 is divided by -5, which is also -2. Finally, the expression 5%7 calculates the remainder when 5 is divided by 7, which is 5. Therefore, the second part of the output is -2,-2,5.
Combining the two parts, the output of the program is 2,2,-2,-2,5.
20.
What is the output of following program? void main(){ int a=2; switch(a) { case 1: printf("A"); break; case 2: printf("B"); continue; case 3: printf("C"); break; case 4; printf("D"); default: printf("E"); }}
Correct Answer
A. ERROR
Explanation
The program will result in an error because there is a syntax error in the code. The case statement for the value 4 is written as "case 4;" instead of "case 4:". The missing colon after the value 4 will cause a compilation error. Therefore, the correct answer is "ERROR".
21.
What is the output of the following program? void main(){ int a; a=1; a++ * ++a; printf("%d",a);}
Correct Answer
A. 3
Explanation
The output of the program is 3.
In the expression "a++ * ++a", the value of "a" is first incremented by 1 (a++), so "a" becomes 2. Then, it is incremented again by 1 (++a), so "a" becomes 3.
The multiplication operation is then performed between the original value of "a" (2) and the incremented value of "a" (3), resulting in 6. However, since the result of the expression is not assigned to any variable, it is not stored or used in the program.
Finally, the value of "a" (3) is printed using the printf statement, resulting in the output of 3.
22.
Which of the following is integral data type?
Correct Answer
A. Char
Explanation
In c char is integral data type. It stores the ASCII value of any character constant.
23.
What is the output of following program? void main(){ printf("%d%d%d",-10^9, -10|9, -10&9);}
Correct Answer
A. -1 -1 0
Explanation
The program uses the bitwise operators ^, |, and & to perform operations on the numbers -10 and 9.
-10^9 performs a bitwise XOR operation between -10 and 9, resulting in -1.
-10|9 performs a bitwise OR operation between -10 and 9, resulting in -1.
-10&9 performs a bitwise AND operation between -10 and 9, resulting in 0.
Therefore, the output of the program is -1 -1 0.
24.
What will be the output of the following ?#include <stdio.h>int main(){char *str []={"AAAAA","BBBBB","CCCCC","DDDDD"};char **sptr []={str+3,str+2,str+1,str};char ***pp; pp=sptr;++pp;printf("%s",**++pp+2);return 0;}
Correct Answer
A. BBB
Explanation
The code declares a character array `str` with 4 elements, each containing a string of 5 characters. It also declares a pointer array `sptr` with 4 elements, each pointing to one of the strings in `str` in reverse order. It then declares a triple pointer `pp` and assigns it the address of the first element of `sptr`.
The expression `**++pp+2` increments `pp` to point to the second element of `sptr`, then dereferences twice to get the value at that memory location, which is the string "BBBBB". Adding 2 to this string gives "BBB".
Therefore, the output will be "BBB".
25.
What is the output of following program? void main(){ int a; a=10; a*=10+2; printf("%d",a);}
Correct Answer
A. 120
Explanation
The program initializes the variable 'a' to 10. Then, it multiplies 'a' by the expression '10+2', which evaluates to 12. Therefore, the value of 'a' becomes 10 * 12 = 120. Finally, the program prints the value of 'a', which is 120.
26.
What will be output when you will execute following code? #include "stdio.h"int main(){ char a=250; int expr; expr= a+ !a + ~a + ++a; printf("%d",expr); return 0;}
Correct Answer
A. -6
Explanation
char a = 250;
250 is beyond the range of signed char. Its corresponding cyclic value is: -6
So, a = -6
Consider on the expression:
expr= a+ !a + ~a + ++a;
Operator! , ~ and ++ have equal precedence. And it associative is right to left.
So, First ++ operator will perform the operation. So value a will -5
Now,
Expr = -5 + !-5 + ~-5 + -5
= -5 + !-5 + 4 - 5
= -5 + 0 + 4 -5
= -6
27.
What will be the Output of the following ?#include <stdio.h>int main(){char ch=10;void *ptr=&ch;printf("%d,%d",*(char*)ptr,++(*(char*)ptr));return 0;}
Correct Answer
A. 11,11
Explanation
The program declares a character variable 'ch' and initializes it with the value 10. Then, a void pointer 'ptr' is declared and assigned the address of 'ch'. The printf statement prints the value at the address pointed by 'ptr' (which is 10) and then increments the value at that address by 1 (to 11). Therefore, the output will be "10,11".
28.
What is the output of following program? void main(){ int i=10; printf("%d%d%d",++i, i++, ++i);}
Correct Answer
A. 13 11 11
Explanation
The output of the program is 13 11 11. This is because the order of evaluation of the expressions in the printf statement is not defined. In this case, the behavior is undefined and can vary depending on the compiler.
29.
What will the Output of the following? main() { static int var = 5; printf("%d ",var--); if(var) main(); }
Correct Answer
A. 54321
Explanation
The program starts by printing the value of the static variable "var", which is initially 5. Then, it decrements the value of "var" by 1. After that, it checks if "var" is still non-zero. Since "var" is now 4, the condition is true and the program calls the "main()" function again. This process continues until "var" becomes 0. Each time the "main()" function is called recursively, the value of "var" is printed and decremented. Therefore, the output will be 54321.
30.
What will be output when you will execute following code? void main(){ if(!10>-10) printf("C"); else printf("C++");}
Correct Answer
A. C
Explanation
The output of the code will be "C". This is because the condition in the if statement is "!10>-10", which evaluates to "true". Therefore, the code inside the if block will be executed, and "C" will be printed on the screen.