1.
What is the output of this program?#include int main(){int a = 0;a = 1 + (a++);printf("%d", a);return 0;}
Correct Answer
B. 1
Explanation
In this program, the variable 'a' is initially assigned the value 0. Then, the expression '1 + (a++)' is evaluated. The 'a++' part increments the value of 'a' by 1, but since it is a post-increment operator, the value of 'a' used in the expression is still 0. Therefore, the expression becomes '1 + 0', resulting in the value 1. Finally, the program prints the value of 'a', which is 1.
2.
What is the output of this program?int main(){int a = 0;for(; a <= 10; a++);printf("%d", a);}
Correct Answer
B. 11
Explanation
The program initializes the variable "a" to 0. It then enters a for loop that continues as long as "a" is less than or equal to 10. In each iteration of the loop, "a" is incremented by 1. After the loop ends, the program prints the value of "a" which is 11. Therefore, the output of the program is 11.
3.
What is the output of this program?int main(){int a = 0;for(a = 0; a <= 10; a++)for( a = 0; a <= 10; a++);printf("%d", a);return 0;}
Correct Answer
B. 12
Explanation
The program initializes the variable 'a' to 0. Then, it enters a nested for loop where it sets 'a' to 0 and checks if it is less than or equal to 10. Inside this nested loop, it increments 'a' by 1. However, there is an extra semicolon at the end of the nested for loop, causing it to be an empty loop. After the nested loop, it prints the value of 'a', which is 12 because the outer loop is skipped due to the extra semicolon. Therefore, the output of the program is 12.
4.
What is the output of this program?int main(){int a = 0;for(a = 0; a <= 10; a++)for( a = 0; a <= 10; a++)for(a = 0; a <= 100; a++); printf("%d", a);return 0;}
Correct Answer
B. 101
Explanation
The output of the program is 101. This is because the program uses nested for loops to increment the value of 'a'. The first loop initializes 'a' to 0 and continues as long as 'a' is less than or equal to 10. The second loop also initializes 'a' to 0 and continues as long as 'a' is less than or equal to 10. The third loop initializes 'a' to 0 and continues as long as 'a' is less than or equal to 100, but it does not have any statements inside the loop. After the third loop, the printf statement is executed, which prints the value of 'a' which is 101.
5.
What is the output of this program?void rec(int);int main(){int a = 10;rec(10);return 0;}void rec(int a){if(a == 0)return;printf("%d", a);rec(--a);}
Correct Answer
A. 10987654321
Explanation
The program starts by calling the function rec(10) from the main function. The rec function takes an integer parameter 'a'. Inside the rec function, it checks if 'a' is equal to 0, and if so, it returns. Otherwise, it prints the value of 'a' and then recursively calls the rec function with 'a' decremented by 1. This process continues until 'a' becomes 0. So, the program will print the numbers from 10 to 1 in descending order, resulting in the output "10987654321".
6.
What is the output this program?int main(){int a = -10;while(a){a++;}printf("%d", a);return 0;}
Correct Answer
B. 0
Explanation
The program initializes the variable "a" with a value of -10. The while loop runs as long as "a" is non-zero, which means it will continue until "a" becomes 0. Inside the loop, "a" is incremented by 1 on each iteration. Since "a" starts at -10 and is incremented by 1 each time, it will eventually reach 0 after 10 iterations of the loop. Therefore, the output of the program will be 0.
7.
What is the output of this program?int main(){int a = 65;char c = a;printf("%d %c", c, a);}
Correct Answer
A. 65 A
Explanation
The program initializes an integer variable 'a' with the value 65. Then, it assigns the value of 'a' to a character variable 'c'. Since the ASCII value of 65 corresponds to the character 'A', 'c' will hold the value 'A'. The printf statement then prints the value of 'c' followed by the value of 'a', resulting in the output "65 A".
8.
What is the output of this C snippet?int main(){int a = 65;char c = a;int d = a + c;printf("%d", d);}
Correct Answer
A. 130
Explanation
In this C snippet, the variable "a" is assigned the value 65. Then, the variable "c" is assigned the value of "a", which is the character 'A' in ASCII. Next, the variable "d" is assigned the sum of "a" and "c", which is 65 + 65 = 130. Finally, the value of "d" is printed, resulting in the output of 130.
9.
What is the output of this program?void main(){int k;for (k = -3; k < -5; k++)printf("Hello");}
Correct Answer
D. Nothing
Explanation
The program initializes the variable k to -3 and enters a for loop. The condition for the loop is that k is less than -5, which is not true since k is -3. Therefore, the loop does not execute and the program does not print anything. Hence, the output of the program is "Nothing".
10.
What is the output of this C snippet?int main(){int i = 0;for (i++; i == 1; i = 2)printf("%d ", i);printf("%d", i);}
Correct Answer
A. 1 2
Explanation
The given C snippet includes a for loop that has three parts: initialization (i++), condition (i == 1), and increment (i = 2).
Initially, the value of i is 0. In the first iteration, i is incremented to 1. Since the condition i == 1 is true, the loop body is executed, and the value of i (which is 2) is printed.
In the next iteration, i is again incremented to 3. However, the condition i == 1 is false, so the loop is terminated.
Finally, outside the loop, the value of i (which is 2) is printed.
Therefore, the output of this C snippet is 1 2.
11.
What is the output of this C code?int main(){int x = 2, y = 2;float f = y + x /= x / y;printf("%d %f\n", x, f);return 0;}
Correct Answer
A. Compile time error
Explanation
The given C code will result in a compile-time error. This is because the expression "y + x /= x / y" violates the order of operations in C. The division operation "x / y" is evaluated first, which results in 1. Then, the compound assignment operator "/=" is applied to "x" and the result of the division, which is not allowed. Hence, the code will fail to compile.
12.
What is the output of this C code?int main(){int x = 1, y = 2;if (x && y == 1)printf("true\n");elseprintf("false\n");}
Correct Answer
A. False
Explanation
The code snippet declares two variables, x and y, with initial values of 1 and 2 respectively. The if statement checks if both x and y satisfy the condition x && y == 1. Since x is not equal to zero, the condition evaluates to true. However, y is not equal to 1, so the condition as a whole evaluates to false. Therefore, the else block is executed and "false" is printed as the output.
13.
What is the output of this C code?int main(){int x = 1, y = 2;int z = x & y == 2;printf("%d\n", z);}
Correct Answer
B. 1
Explanation
The output of this C code is 1. The code declares three variables: x with a value of 1, y with a value of 2, and z. The expression "x & y == 2" is evaluated using the bitwise AND operator "&". Since the bitwise AND operator has higher precedence than the equality operator "==", the expression is equivalent to "x & (y == 2)". The equality operator compares the value of y to 2, which is true, resulting in 1. Then, the bitwise AND operator is applied to 1 and 1, resulting in 1. Finally, the value of z, which is 1, is printed.
14.
What is the output of this C code?int main(){int x = 0, y = 2;if (!x && y)printf("true\n");elseprintf("false\n");}
Correct Answer
A. True
Explanation
The code snippet initializes two variables, x and y, with values 0 and 2 respectively. The condition in the if statement checks if the logical NOT of x is true (which is false) and if y is true (which is true). Since both conditions are true, the code will execute the printf statement "true". Therefore, the output of this code will be "true".
15.
What is the output of this C code?void main(){int k = 4;float k = 4;printf("%d", k)}
Correct Answer
A. Compile time error
Explanation
The code will result in a compile time error because it is declaring two variables with the same name "k" but with different data types (int and float). This is not allowed in C programming.
16.
A variable declared in a function can be used in main.
Correct Answer
B. False
Explanation
A variable declared in a function is local to that function and cannot be accessed outside of it, including in the main function. Therefore, the statement "A variable declared in a function can be used in main" is false.
17.
What is the output of this C code?int main(){int x = 2, y = 0;int z = (y++) ? y == 1 && x : 0;printf("%d\n", z);return 0;}
Correct Answer
A. 0
Explanation
In this C code, the variable "x" is assigned the value of 2 and the variable "y" is assigned the value of 0. The variable "z" is then assigned the value of 0 because the expression "(y++)" evaluates to false (0). Since the expression is false, the ternary operator evaluates the second operand, which is "0". Therefore, the output of this code is 0.
18.
What is the output of this C code?#includeint main(){int x = 1;short int i = 2;float f = 3;if (sizeof((x == 2) ? f : i) == sizeof(float))printf("float\n");else if (sizeof((x == 2) ? f : i) == sizeof(short int))printf("short int\n");}
Correct Answer
A. Float
Explanation
The output of this C code is "float". This is because the conditional expression `(x == 2) ? f : i` is evaluated at runtime. Since `x` is not equal to 2, the expression evaluates to `i`, which is a short int. The `sizeof` operator is then used to determine the size of the expression. Since the size of `i` is not equal to the size of a float, the first if statement is not executed. The second if statement is then executed, which prints "float" as the output.
19.
What is the output of this C code?int main(){int a = 2;int b = 0;int y = (b == 0) ? a :(a > b) ? (b = 1): a;printf("%d\n", y);}
Correct Answer
C. 2
Explanation
The code initializes three variables: a with the value 2, b with the value 0, and y with the result of a conditional expression. The conditional expression checks if b is equal to 0. Since it is, the expression evaluates to a. Therefore, the value of y is 2. The code then prints the value of y, which is 2.
20.
What is the output of this C code?void main(){1 < 2 ? return 1 : return 2;}
Correct Answer
A. Returns 1
Explanation
The code snippet uses the ternary operator to check if 1 is less than 2. Since this condition is always true, the expression after the '?' is executed, which is "return 1". Therefore, the output of the code is 1.
21.
What is the output of this C code?int main(){switch (printf("Do")){case 1:printf("First\n");break;case 2:printf("Second\n");break;default:printf("Default\n");break;}}
Correct Answer
C. DoSecond
Explanation
The code first calls the printf function with the string "Do" as an argument. The printf function returns the number of characters printed, which in this case is 2. The switch statement then checks the returned value. Since it is 2, the case 2 is executed, which prints "Second" followed by a line break. Therefore, the output of the code is "DoSecond".
22.
Comment on the output of this C Code?int main(){int a = 1;switch (a){case 1:printf("%d", a);case 2:printf("%d", a);case 3:printf("%d", a);default:printf("%d", a);}}
Correct Answer
A. No error, output is 1111
Explanation
The code does not have any syntax errors and will compile successfully. The output of the code will be "1111" because there are no break statements after each case in the switch statement. As a result, once the first case is matched, all subsequent cases will also be executed.
23.
Switch statement accepts _______.
Correct Answer
D. All of the mentioned
Explanation
A switch statement in programming accepts multiple data types, including int, char, and long. Therefore, the correct answer is "All of the mentioned." This means that a switch statement can be used with any of these data types as input.
24.
Comment on the output of this C code?int main(){int a = 1;switch (a){case a:printf("Case A ");default:printf("Default");}}
Correct Answer
D. Compile time Error
Explanation
The given code will result in a compile-time error. In the switch statement, the cases should be constants or literal values, not variables. Therefore, using "a" as a case label is invalid. The code should be modified to use constant values as case labels, such as "case 1" instead of "case a".
25.
What is the output of this code having void return-type function?void foo(){return 1;}void main(){int x = 0;x = foo();printf("%d", x);}
Correct Answer
D. Compile time error
Explanation
The code is attempting to return an integer value (1) from a function with a void return type (foo). This is not allowed and will result in a compile time error. The correct answer is Compile time error.
26.
What will be the data type returned for the following function?int func(){return (double)(char)5.0;}
Correct Answer
B. Int
Explanation
The function is returning the result of multiple type-casting operations. First, the number 5.0 is casted to a char, resulting in the ASCII value of 5. Then, this char value is casted to a double. Finally, the double value is returned as an int. Since the final return type is int, the data type returned for the function is int.
27.
What is the output of this C code?int main(){void foo();printf("1 ");foo();}void foo(){printf("2 ");}
Correct Answer
A. 1 2
Explanation
The output of this C code is "1 2". The code first calls the main function, which then calls the foo function. Inside the foo function, "2" is printed. After the foo function is executed, the main function continues and prints "1". Therefore, the overall output is "1 2".
28.
What is the output of this C code?int main(){void foo(), f();f();}void foo(){printf("2 ");}void f(){printf("1 ");foo();}
Correct Answer
B. 1 2
Explanation
The code first declares two functions, foo() and f(). In the main() function, f() is called, which prints "1 ". Then, within f(), foo() is called, which prints "2 ". Therefore, the output of the code is "1 2".
29.
What is the output of this C code?void m(){printf("hi");}void main(){m();}
Correct Answer
A. Hi
Explanation
The given C code defines a function "m()" which prints "hi" using the printf() function. The main() function calls the m() function. Therefore, when the code is executed, it will output "hi".
30.
What is the output of this C code?void main(){int n = 0, m = 0;if (n > 0)if (m > 0)printf("True");elseprintf("False");}
Correct Answer
C. No output will be printed
Explanation
The code snippet defines two variables, n and m, both initialized to 0. The code then checks if n is greater than 0. Since n is 0, the condition is false and the code does not execute the inner if statement. Therefore, no output will be printed.
31.
What is the output of this program?main(){ if (sizeof(int) > -1) printf("True"); else printf("False");}
Correct Answer
B. False
Explanation
The output of this program is "False" because the condition in the if statement is false. The sizeof(int) function returns the size in bytes of the data type int, which is always a positive value. Therefore, the condition sizeof(int) > -1 will always be true, and the program will print "True".
32.
Comment on the output?main(){ char *p = 0; *p = 'a'; printf("value in pointer p is %c\n", *p);}
Correct Answer
D. Runtime error
Explanation
The given code snippet will result in a runtime error. This is because the pointer variable 'p' is assigned a null value (0) and then it is dereferenced to assign the character 'a' to the memory location it points to. However, since 'p' is pointing to null, it does not have a valid memory location to store the value 'a', causing a runtime error.
33.
Which of the following are unary operators?
Correct Answer
D. All of the mentioned
Explanation
The correct answer is "All of the mentioned". The question asks which of the options are unary operators. A unary operator is an operator that operates on a single operand. In this case, all three options - sizeof, - (negation), and ++ (increment) - are unary operators because they each operate on a single operand. Therefore, the correct answer is that all of the mentioned options are unary operators.
34.
Which of the following method are accepted for assignment?
Correct Answer
B. A = b = c = d = 5;
Explanation
The correct answer is "a = b = c = d = 5;". This is because in this method, the value of 5 is assigned to all variables a, b, c, and d in a single line. This is a valid assignment syntax in many programming languages.
35.
Which of the following operators has the lowest precedence?
Correct Answer
D. ,
Explanation
The operator with the lowest precedence is the comma operator (,). This operator is used to separate expressions and evaluate them from left to right. It has the lowest precedence, meaning it is evaluated last among the given operators. Therefore, the comma operator has the lowest priority in terms of precedence.
36.
Comment on the output of this C code?int main(){ int x = 3, i = 0; do { x = x++; i++; } while (i != 3); printf("%d\n", x);}
Correct Answer
A. Output will be 3
Explanation
The output of this C code will be 3.
In the do-while loop, the variable x is assigned the value of x++ which is a post-increment operation. This means that the current value of x is assigned to x, and then the value of x is incremented.
Since the initial value of x is 3, the assignment statement x = x++ will assign the value 3 to x and then increment it to 4.
However, the value of x is not used or updated in the condition of the do-while loop, which is i != 3. Therefore, the loop will only execute once, and the value of x will remain 3.
Hence, the output of the code will be 3.
37.
What is the output of this C code?int main(){ int a = -1, b = 4, c = 1, d; d = ++a && ++b || ++c; printf("%d, %d, %d, %d\n", a, b, c, d); return 0;}
Correct Answer
A. 0, 4, 2, 1
Explanation
The code initializes variables a, b, c, and d with values -1, 4, 1, and 0 respectively. The expression ++a && ++b || ++c is evaluated. Since a is incremented before the evaluation, it becomes 0. Since 0 is considered false in C, the evaluation stops at that point and the value of d remains 0. The values of a, b, and c remain unchanged. Therefore, the output is 0, 4, 1, 0.
38.
What is the output of this C code?int main(){ int p = 10, q = 20, r; if (r = p = 5 || q > 20) printf("%d", r); else printf("No Output\n");}
Correct Answer
A. 1
Explanation
The output of this C code is 1. In the if statement, the expression "r = p = 5 || q > 20" is evaluated. The logical OR operator || is used, which means that if either expression "p = 5" or "q > 20" is true, the whole expression will be true. In this case, "p = 5" is true because the assignment operator = assigns the value 5 to p. Therefore, the whole expression is true and the value of r is 1. The printf statement then prints the value of r, which is 1.
39.
What is the output of this C code?void main(){ char *str = ""; do { printf("hello"); } while (str);}
Correct Answer
D. Hello is printed infinite times
Explanation
The code declares a character pointer variable `str` and assigns it an empty string. The code then enters a do-while loop, which will always execute at least once. Inside the loop, the string "hello" is printed. Since the condition for the loop is `str`, which is a non-null pointer, the loop will continue to execute indefinitely, resulting in "hello" being printed infinite times.
40.
Example of iteration in C
Correct Answer
D. All of the mentioned
Explanation
All of the mentioned options (for, while, do-while) are examples of iteration in the C programming language. Iteration is a process of repeatedly executing a block of code until a certain condition is met. The "for" loop is used when the number of iterations is known beforehand. The "while" loop is used when the number of iterations is not known beforehand, but a condition needs to be checked before each iteration. The "do-while" loop is similar to the "while" loop, but it checks the condition after each iteration. Therefore, all of the mentioned options are valid examples of iteration in C.
41.
Number of times while loop condition is tested is, i is initialized to 0 in both case.while(i < n) i++;_ _ _ _ _ _ _ _ _do i++;while(i <= n)
Correct Answer
D. N + 1, n + 1
Explanation
The given code snippet consists of two loops. The first loop is a while loop that increments the value of i until it becomes greater than n. The second loop is a do-while loop that also increments the value of i until it becomes greater than or equal to n.
In both cases, the initial value of i is 0.
In the first loop, the condition is tested n times because i starts at 0 and is incremented n times until it becomes greater than n.
In the second loop, the condition is tested n+1 times because i starts at 0 and is incremented n+1 times until it becomes greater than or equal to n.
Therefore, the correct answer is n + 1, n + 1.
42.
What is the output of this C code?int main(){ int i = 0; while (i = 0) printf("True\n"); printf("False\n");}
Correct Answer
C. False
Explanation
The code will output "False". The while loop condition is "i = 0", which is an assignment rather than a comparison. Since the value of "i" is initially 0, the assignment will always evaluate to true, causing an infinite loop. However, since there is no break or exit condition within the loop, the program will not reach the second printf statement, resulting in only "True" being printed an infinite number of times. Therefore, the correct answer is "False".
43.
Which loop is more suitable for first perform the operation and then test the condition?
Correct Answer
C. Do-while loop
Explanation
The do-while loop is more suitable for first performing the operation and then testing the condition. This is because the do-while loop executes the code block at least once before checking the condition. Therefore, it guarantees that the operation will be performed at least once, regardless of the condition. In contrast, the for loop and while loop first test the condition before executing the code block, so they may not execute the operation if the condition is initially false.
44.
What is the output of this C code?int main(){ int x = 1; if (x > 0) printf("inside if "); else if (x > 0) printf("inside elseif ");}
Correct Answer
A. Inside if
Explanation
The code will output "inside if" because the condition in the if statement (x > 0) is true. The else if statement will not be executed because the condition (x > 0) is not true.
45.
What is the output of this C code?int main(){ int x = 0; if (x++) printf("true\n"); else if (x == 1) printf("false\n");}
Correct Answer
B. False
Explanation
The output of this C code is "false". The code starts with initializing the variable "x" to 0. The if statement checks the condition "x++", which means it checks the value of "x" before incrementing it. Since the initial value of "x" is 0, it evaluates to false and the code moves to the next else if statement. Here, it checks if "x" is equal to 1, which is true. Therefore, it executes the printf statement and prints "false".
46.
What is the output of this C code?int main(){ int x = 0; if (x == 1) if (x == 0) printf("inside if\n"); else printf("inside else if\n"); else printf("inside else\n");}
Correct Answer
B. Inside else
Explanation
The code starts by initializing the variable x to 0. The first if statement checks if x is equal to 1, which is false. Therefore, the code skips the first if statement and moves on to the else statement. The else statement prints "inside else". So, the output of this code is "inside else".
47.
What is the output of this C code?int main(){ int x = 0; if (x == 0) printf("true, "); else if (x = 10) printf("false, "); printf("%d\n", x);}
Correct Answer
C. True, 0
Explanation
The code first initializes the variable x to 0. Then it enters the if statement and since x is equal to 0, it prints "true, ". After that, it proceeds to the next printf statement and prints the value of x which is 0. Therefore, the output of the code is "true, 0".
48.
If(a == 1 || b == 2){} can be mentioned as
Correct Answer
D. None of the mentioned
49.
Which of the following is an invalid if-else statement?
Correct Answer
A. If(if(a == 1)){}
Explanation
The given if-else statement "if(if(a == 1)){}" is invalid because the condition inside the outer if statement is another if statement, which is not allowed. The condition inside an if statement should be a boolean expression, but in this case, it is another if statement, which is not a valid boolean expression.
50.
What is the output of this C code?int main(){ int a = 1; if (a--) printf("True"); if (a++) printf("False");}
Correct Answer
A. True
Explanation
The code initializes the variable "a" with the value 1. In the first if statement, "a--" is evaluated, which means the value of "a" is used in the condition and then decremented by 1. Since the initial value of "a" is 1, the condition is true and "True" is printed. In the second if statement, "a++" is evaluated, which means the value of "a" is used in the condition and then incremented by 1. Since the value of "a" is now 0, the condition is false and "False" is not printed. Therefore, the output of the code is "True".