1.
# include int one_d[]={1,2,3};main(){int *ptr;ptr=one_d;ptr+=3;printf("%d",*ptr);
Correct Answer
E. Compilation error
Explanation
The given code will result in a compilation error. This is because the code is missing the required header file, which is "stdio.h" for the printf() function.
2.
Main(){char s[ ]="man";int i;for(i=0;s[ i ];i++)printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);}What will be the second output of printf?
Correct Answer
B. Aaaa
Explanation
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array
name is the base address for that array. Here s is the base address. i is the index number/displacement from
the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is
same as s[i].
3.
Main(){int a[10];printf("%d",*a+1-*a+3);
Correct Answer
D. 4
Explanation
The given code declares an integer array 'a' of size 10. In the printf statement, the expression *a+1-*a+3 is evaluated. Since 'a' is an array, *a refers to the value at the first index of the array, which is the same as a[0]. So, *a+1 is equivalent to a[0]+1. Similarly, *a+3 is equivalent to a[0]+3. Therefore, the expression simplifies to a[0]+1-a[0]+3, which further simplifies to 1+3, resulting in the value 4 being printed.
4.
Main(){float me = 1.1;double you = 1.1;if(me==you)printf("I love U");elseprintf("I hate U");}
Correct Answer
B. I hate U
Explanation
Explanation:
For floating point numbers (float, double, long double) the values cannot be predicted
exactly. Depending on the number of bytes, the precession with of the value represented varies. Float
takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with relational
operators (== , >,
5.
Main(){int i=3;switch(i){default:printf("zero");case 1: printf("one");break;case 2:printf("two");break;case 3: printf("three");break;}}
Correct Answer
C. Three
Explanation
* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H.
6.
Main(){int i=10;i=!i>14;Printf ("i=%d",i);}
Correct Answer
C. 0
Explanation
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a
unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).
7.
Main(){printf("\nab");printf("\bsi");printf("\rha");}
Correct Answer
D. Hai
Explanation
\n - newline
\b - backspace
\r - linefeed
8.
#include #define a 10main(){#define a 50printf("%d",a);}
Correct Answer
D. 50
Explanation
Runtime error-Stack overflow.main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error.
9.
#includemain(){int i=1,j=2;switch(i){case 1: printf("GOOD");break;case j: printf("BAD");break;}}
Correct Answer
C. Compilation error.
Explanation
Explanation:
The case statement can have only constant expressions (this implies that we cannot use
variable names directly so an error).
Note:
Enumerated types can be used in case statements.
10.
Main(){int i=0;for(;i++;printf("%d",i)) ;printf("%d",i);}
Correct Answer
B. 1
Explanation
The code snippet is a C program that starts with initializing an integer variable i to 0. It then enters a for loop with an empty initialization statement, a condition of i++, and an empty increment statement. This means that i will be incremented after each iteration of the loop. Inside the loop, the printf function is used to print the value of i. However, since the condition i++ is always evaluated as true, the loop becomes an infinite loop. Therefore, the program will continuously print the incremented value of i, starting from 1. Hence, the correct answer is 1.
11.
Main(){char not;not=!2;printf("%d",not);}
Correct Answer
C. 0
Explanation
The code snippet initializes a variable 'not' as a character. The expression '!2' evaluates to 0, as the logical NOT operator (!) negates the truth value of the operand. The 'printf' function then prints the value of 'not', which is 0. Therefore, the correct answer is 0.
12.
#define FALSE -1#define TRUE 1#define NULL 0main() {if(NULL)puts("NULL");else if(FALSE)puts("TRUE");elseputs("FALSE");}
Correct Answer
B. TRUE
Explanation
The code snippet defines FALSE as -1, TRUE as 1, and NULL as 0. In the main function, the if statement checks if NULL is true. Since NULL is defined as 0, which is considered false, the if statement evaluates to false. The else if statement then checks if FALSE is true. Since FALSE is defined as -1, which is considered true, the else if statement evaluates to true. Therefore, the code will print "TRUE" as the output.
13.
Void main(){ int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n");}
Correct Answer
B. Ok here
Explanation
Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed.
14.
Main(){int *j;{int i=10;j=&i;}printf("%d",*j);}
Correct Answer
A. 10
Explanation
Due to the assignment p[1] = 'c' the string becomes, "%c\n". Since this string becomes the format string for printf and ASCII value of 65 is 'A', the same gets printed.
15.
# include
aaa() {
printf("hi");
}
bbb(){
printf("hello");
}
ccc(){
printf("bye");
}
main()
{
int (*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
Correct Answer
A. Bye
Explanation
The given code snippet defines three functions: aaa, bbb, and ccc. These functions each print a different greeting message. In the main function, an array of function pointers called ptr is declared. The elements of this array are then assigned the addresses of the aaa, bbb, and ccc functions. Finally, the function pointed to by ptr[2] is called, which is ccc. Therefore, the output of the program would be "bye".
16.
Register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a);}
Correct Answer
A. Compilation error
Explanation
Compier Error: '&' on register variable
Rule to Remember:
& (address of ) operator cannot be applied on register variables.