1.
Which of the following is not a valid variable name declaration?
Correct Answer
B. Int 3_a;
Explanation
The variable name "3_a" is not a valid variable name declaration because it starts with a number. In most programming languages, variable names cannot start with a number, they must start with a letter or an underscore. Therefore, "int 3_a;" is not a valid declaration.
2.
Which is valid C expression ?
Correct Answer
D. Int my_num = 100000;
Explanation
The valid C expression is "int my_num = 100000;". This is because it follows the correct syntax for declaring and initializing an integer variable in C. The variable name "my_num" is valid, and it is assigned the value of 100000. The other options either have invalid variable names or incorrect syntax, such as the use of a comma or a space in the variable name.
3.
What is the output of this C code? #include <stdio.h> int main() { int y = 10000; int y = 34; printf("Hello World! %d\n", y); return 0; }
Correct Answer
C. Compile time error
Explanation
The code will result in a compile-time error because it is trying to declare the variable "y" twice. In C, each variable must have a unique name within its scope. Since the code is trying to declare "y" twice, the compiler will throw an error.
4.
How many times below for loop will be executed ?#include<stdio.h>int main(){ int i=0; for(;;) printf("%d",i); return 0;}
Correct Answer
B. Infinite times
Explanation
The for loop in the given code does not have any condition specified in the middle section. This means that the loop will continue to execute indefinitely until it is explicitly terminated using a break statement or some other means. Therefore, the loop will be executed infinite times.
5.
Find output of the following programme ?#include<stdio.h>int main(){ char str[] = "Smaller"; int a = 100; printf(a > 10 ? "Greater" : "%s", str); return 0;}
Correct Answer
B. Greater
Explanation
The output of the program will be "Greater". This is because the program uses a ternary operator to check if the value of 'a' is greater than 10. Since 'a' is equal to 100, which is greater than 10, the condition is true and the program will print "Greater".
6.
Guess the output of the following programme ?#include<stdio.h>int main(){ int a = 100, b = 200, c = 300; if(!a >= 500) b = 300; c = 400; printf("%d,%d,%d",a, b, c); return 0;}
Correct Answer
C. 100,200,400
Explanation
The program initializes three variables, a, b, and c, with values 100, 200, and 300 respectively. The if statement checks if the logical NOT of a greater than or equal to 500 is true. Since a is 100, which is not greater than or equal to 500, the condition evaluates to true. Therefore, the statement b = 300; is not executed. The statement c = 400; is executed regardless of the if condition. Finally, the printf statement prints the values of a, b, and c which are 100, 200, and 400 respectively. Hence, the output of the program is 100, 200, 400.
7.
Guess the output of the following programme ?#include<stdio.h>int main(){ int x = 10; float y = 10.0; if(x == y) printf("x and y are equal"); else printf("x and y are not equal"); return 0;}
Correct Answer
B. X and y are equal
Explanation
The program compares the integer variable x with the float variable y using the equality operator. Since the values of x and y are both 10, the condition x == y is true. Therefore, the program will output "x and y are equal".
8.
We can not use string in the switch case as label ?
Correct Answer
A. True
Explanation
In a switch case statement, the labels used must be constant expressions that can be evaluated at compile time. Since strings are not considered constant expressions in most programming languages, they cannot be used as labels in a switch case. Instead, switch cases typically use integer or character constants as labels. Therefore, the statement "We can not use string in the switch case as label" is true.
9.
Which of the following can not be checked in a switch case statement ?
Correct Answer
B. Float
Explanation
A switch case statement can be used to check for integer values, character values, and enumerated values. However, it cannot be used to check for float values. This is because switch case statements can only evaluate discrete values, and floating-point numbers are continuous values. Therefore, float cannot be checked in a switch case statement.
10.
Continue statement is allowed inside the switch case statement ?
Correct Answer
B. No
Explanation
The answer is "No" because the switch case statement does not allow the use of the continue statement. The continue statement is used to skip the rest of the code in a loop and move to the next iteration, but it cannot be used inside a switch case statement. Instead, the break statement is used to exit the switch case block.
11.
What will be the valid value of x by which we can print "DEF" ?#include<stdio.h>int main(){int x = _________ ; if(x) printf("ABC"); else printf("DEF"); return 0;}
Correct Answer
A. 0
Explanation
In the given code, the condition in the if statement is checking the value of x. If the value of x is non-zero, the condition will be true and "ABC" will be printed. However, if the value of x is zero, the condition will be false and "DEF" will be printed. Therefore, in order to print "DEF", the valid value of x should be 0.
12.
What is the output of this C code? #include <stdio.h> 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 will be "false". The code initializes the variable x to 0. In the if statement, x is incremented using the post-increment operator, but the value of x is still 0 at the time of evaluation. Since the condition in the if statement is false, the code moves to the else if statement. Here, x is compared to 1, and since x is still 0, the condition is also false. Therefore, the code reaches the end without printing anything.
13.
What is the output of this C code? #include <stdio.h> 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
C. Inside else
Explanation
The code snippet checks the value of the variable x. Since x is initially assigned a value of 0, the condition "if (x == 1)" evaluates to false. Therefore, the code moves to the "else" block and prints "inside else".
14.
The control automatically passes to the first statement after the loop in
Correct Answer
B. Continue statement
Explanation
The continue statement is used in loops to skip the rest of the current iteration and move on to the next iteration. Therefore, when the continue statement is encountered, the control automatically passes to the first statement after the loop. This allows the program to skip any remaining code within the loop and proceed with the next iteration.
15.
Consider the following C code:{int a=5,b=9;float r;r=b/a;}What is the value of r?
Correct Answer
B. 1.0
Explanation
The value of r is 1.0 because in the given code, the variables a and b are both integers, so when dividing b by a, the result will also be an integer. In this case, 9 divided by 5 equals 1.8, but since r is declared as a float, the decimal part is truncated and the value of r becomes 1.0.
16.
What is the output of this C code? #include <stdio.h> void main() { double k = 0; for (k = 0.0; k < 3.0; k++); printf("%lf", k); }
Correct Answer
C. 3.000000
Explanation
The code initializes a variable 'k' to 0 and then enters a for loop. The loop condition checks if 'k' is less than 3, and if true, increments 'k' by 1. However, the loop body is empty, indicated by the semicolon immediately after the loop declaration. Therefore, the loop runs until 'k' becomes 3, and then the loop terminates. After the loop, the value of 'k' is printed, which is 3.000000.
17.
What is the output of this C code?#include <stdio.h> void main() { int k; for (k = -3; k < -5; k++) printf("Hello"); }
Correct Answer
D. Nothing
Explanation
The output of this C code is "Nothing" because the condition of the for loop is already false. The initial value of k is -3 and the condition is k < -5. Since -3 is not less than -5, the for loop does not execute and the printf statement is not executed. Therefore, there is no output and the program terminates without printing anything.
18.
What is the output of this C code?#include <stdio.h>void main(){ int i = 0; while (i < 10) { i++; printf("hi\n"); while (i < 8) { i++; printf("hello\n"); } }}
Correct Answer
D. Hi is printed once, hello 7 times and then hi 2 times
Explanation
The code starts with initializing the variable i to 0. Then, it enters a while loop that will execute as long as i is less than 10. Inside the loop, i is incremented by 1 and "hi" is printed. Then, it enters another while loop that will execute as long as i is less than 8. Inside this loop, i is incremented by 1 and "hello" is printed. After this inner loop finishes, the control goes back to the outer loop and "hi" is printed again. This process continues until i becomes 10. Therefore, the output will be "Hi" printed once, "hello" printed 7 times, and "hi" printed 2 times.
19.
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
B. N+1, n+1
Explanation
In the first case, the while loop condition is tested n times because i is incremented by 1 each time until it becomes equal to n. In the second case, the do-while loop condition is tested n+1 times because i is incremented by 1 before the condition is checked, and it becomes equal to n+1 when the loop ends. Therefore, the correct answer is n+1, n+1.
20.
What is the output of this C code?#include <stdio.h> int main() { int a = 0, i = 0, b; for (i = 0;i < 5; i++) { a++; continue; } }
Correct Answer
D. 5
21.
What is the output of this C code?#include <stdio.h> int main() { int a = 0, i = 0, b; for (i = 0;i < 5; i++) { a++; if (i == 3) break; } }
Correct Answer
B. 4
Explanation
The output of this C code is 4.
In the for loop, the variable i is initialized to 0 and the loop continues as long as i is less than 5.
Inside the loop, the variable a is incremented by 1 each time.
When i reaches 3, the condition i == 3 is true and the loop is terminated using the break statement.
Therefore, the loop runs 4 times (i=0, i=1, i=2, i=3) and the final value of a is 4.
22.
The keyword ‘break’ cannot be simply used within:
Correct Answer
B. If-else
Explanation
In the if-else statement, the keyword "break" cannot be used because "break" is used to exit a loop or switch statement. In an if-else statement, there is no looping construct, so there is no need to exit from it. Instead, the if-else statement is used to make a decision and execute different blocks of code based on a condition. Therefore, using "break" in an if-else statement would be unnecessary and invalid.
23.
What is the output of this C code?#include <stdio.h> int main() { printf("%d ", 1); goto l1; printf("%d ", 2); l1:goto l2; printf("%d ", 3); l2:printf("%d ", 4); }
Correct Answer
A. 1 4
Explanation
The output of this C code is "1 4".
The code starts by printing "1" using the printf() function. Then, it encounters a "goto" statement which transfers the control to the label "l1". The code then prints "3" using another printf() function. After that, it encounters another "goto" statement which transfers the control to the label "l2". Finally, it prints "4" using the printf() function. So, the output is "1 4".
24.
What is the output of this C code?#include <stdio.h> void main() { int a = 2 + 3 - 4 + 8 - 5 % 4; printf("%d\n", a); }
Correct Answer
B. 8
Explanation
The code calculates the value of the expression "2 + 3 - 4 + 8 - 5 % 4". The modulus operator "%" calculates the remainder of the division operation. In this case, 5 % 4 equals 1. The expression simplifies to "2 + 3 - 4 + 8 - 1", which further simplifies to "8". Therefore, the output of the code is 8.
25.
Scanf () can be used for reading
Correct Answer
C. Multiple character
Explanation
The scanf() function in C can be used for reading multiple characters. It allows the user to input a sequence of characters until a specified delimiter is encountered. This can be useful when reading strings or multiple values from the user. The function scans the input and stores it in the specified variables or buffers. By using appropriate format specifiers, such as %s for strings, scanf() can efficiently read multiple characters from the user.