Explanation:In C, the condition for an if statement must be enclosed in parentheses (). The blocks are enclosed in curly braces {}.
Incorrect! Try again.
2What is the output of the following code snippet?
c
int x = 5;
if (x = 10) {
printf("True");
} else {
printf("False");
}
A.True
B.False
C.Compiler Error
D.Runtime Error
Correct Answer: True
Explanation:The expression x = 10 is an assignment, not a comparison. It assigns 10 to x and evaluates to 10. Since 10 is non-zero, the condition is treated as true.
Incorrect! Try again.
3Which data types are allowed to be used in a switch case label?
A.int and float
B.char and float
C.int and char
D.Any data type
Correct Answer: int and char
Explanation:Switch case labels must be integer constants or constant expressions (which includes char, as characters are stored as integers). Floating-point numbers and strings are not allowed.
Incorrect! Try again.
4What happens if the break statement is missing in a switch case?
A.The compiler throws an error.
B.The program stops execution.
C.Execution falls through to the next case.
D.The switch statement restarts.
Correct Answer: Execution falls through to the next case.
Explanation:Without a break statement, the control flow continues to execute the statements of the subsequent cases regardless of their case labels. This is known as "fall-through".
Incorrect! Try again.
5Which of the following is an exit-controlled loop?
A.for loop
B.while loop
C.do-while loop
D.if-else
Correct Answer: do-while loop
Explanation:A do-while loop evaluates the condition at the end of the loop body, guaranteeing that the body executes at least once.
Incorrect! Try again.
6What is the output of the following loop?
c
for(int i = 0; i < 5; i++);
printf("%d", i);
A.01234
B.5
C.012345
D.Compiler Error
Correct Answer: 5
Explanation:The semicolon ; immediately after the for statement terminates the loop body effectively making it an empty body. After the loop completes, i is 5, and the printf statement executes once.
Incorrect! Try again.
7Which statement is used to skip the current iteration of a loop and proceed to the next iteration?
A.break
B.stop
C.continue
D.return
Correct Answer: continue
Explanation:The continue statement skips the remainder of the current loop iteration and jumps to the update expression (in for loops) or condition check (in while/do-while loops).
Incorrect! Try again.
8In a nested loop, what does the break statement do?
A.Exits all loops.
B.Exits only the innermost loop where it is present.
C.Skips the current iteration.
D.Terminates the program.
Correct Answer: Exits only the innermost loop where it is present.
Explanation:The break statement only terminates the nearest enclosing loop. It does not affect outer loops.
Incorrect! Try again.
9Which format specifier is used for reading a single character in scanf?
A.%s
B.%d
C.%c
D.%f
Correct Answer: %c
Explanation:%c is the standard format specifier for character types in C input/output functions.
Incorrect! Try again.
10What is the return type of the printf() function?
A.void
B.int
C.char
D.float
Correct Answer: int
Explanation:printf() returns an integer representing the total number of characters successfully written to the output.
Incorrect! Try again.
11Consider the expression: What is the value of ?
A.2.5
B.2.0
C.3.0
D.5.0
Correct Answer: 2.0
Explanation:Since both 5 and 2 are integers, integer division is performed, resulting in 2. This integer is then implicitly converted to a float (2.0) during assignment.
Incorrect! Try again.
12Which type modifier allows a variable to hold only positive values?
A.short
B.long
C.signed
D.unsigned
Correct Answer: unsigned
Explanation:The unsigned modifier instructs the compiler to interpret the variable as a non-negative integer, effectively doubling the positive range compared to signed integers.
Incorrect! Try again.
13Which function is safer to read a string containing spaces?
A.scanf("%s", str)
B.gets(str)
C.fgets(str, size, stdin)
D.getchar()
Correct Answer: fgets(str, size, stdin)
Explanation:scanf("%s") stops at whitespace. gets() is dangerous due to buffer overflows. fgets() is the safe alternative that allows specifying the buffer size and reads spaces.
Incorrect! Try again.
14What is the purpose of the default case in a switch statement?
A.To handle the first case.
B.To handle float values.
C.To execute code when no other case matches.
D.It is mandatory for syntax reasons.
Correct Answer: To execute code when no other case matches.
Explanation:The default case executes if none of the constant case labels match the value of the switch expression. It is optional.
Incorrect! Try again.
15What is the correct way to cast an integer x to a float explicitly?
A.float(x)
B.(float)x
C.to_float(x)
D.x.to_float
Correct Answer: (float)x
Explanation:C uses the syntax (type_name) expression for explicit type conversion (type casting).
Incorrect! Try again.
16What is the output of printf("%5.2f", 3.14159);?
A.3.14159
B.3.14
C. 3.14
D.03.14
Correct Answer: 3.14
Explanation:%5.2f specifies a minimum width of 5 characters and 2 decimal places. 3.14 takes 4 characters, so 1 leading space is added to satisfy the width of 5.
Incorrect! Try again.
17Which loop is best suited when the number of iterations is known beforehand?
A.while
B.do-while
C.for
D.goto
Correct Answer: for
Explanation:The for loop syntax (initialization; condition; update) is specifically designed to handle cases where the iteration count or bounds are known.
Incorrect! Try again.
18What does the goto statement require to function?
A.A loop
B.A label
C.A function call
D.A condition
Correct Answer: A label
Explanation:goto transfers control to a labeled statement within the same function. The label is defined by an identifier followed by a colon.
Incorrect! Try again.
19The dangling else problem in C is resolved by associating the else with:
A.The farthest if.
B.The nearest preceding if that is not already matched with an else.
C.The first if in the block.
D.It is resolved randomly.
Correct Answer: The nearest preceding if that is not already matched with an else.
Explanation:In nested if statements without braces, an else is always paired with the closest unmatched if statement above it.
Incorrect! Try again.
20What is the result of the expression 5 % 2?
A.2.5
B.2
C.1
D.0
Correct Answer: 1
Explanation:The modulus operator % returns the remainder of integer division. with a remainder of $1$.
Incorrect! Try again.
21Which escape sequence is used to print a new line?
A.\t
B.\n
C.\b
D.\a
Correct Answer: \n
Explanation:\n represents the newline character, moving the cursor to the beginning of the next line.
Incorrect! Try again.
22Which of the following creates an infinite loop?
A.for(;;)
B.while(1)
C.do { } while(1);
D.All of the above
Correct Answer: All of the above
Explanation:All listed constructs create a loop with a condition that is permanently true (or absent, which implies true in for), resulting in an infinite loop.
Incorrect! Try again.
23What is the return type of scanf()?
A.void
B.float
C.int
D.char *
Correct Answer: int
Explanation:scanf returns the number of input items successfully matched and assigned.
Incorrect! Try again.
24Which function prints a string followed automatically by a newline to stdout?
A.printf()
B.puts()
C.putchar()
D.fputs()
Correct Answer: puts()
Explanation:puts() writes the string to stdout and appends a newline character automatically. printf requires explicit \n.
Incorrect! Try again.
25In the for loop for(init; condition; update), when is the update statement executed?
A.Before the body execution.
B.After the body execution.
C.Before the condition check.
D.Simultaneously with the condition.
Correct Answer: After the body execution.
Explanation:The flow is: Initialization Condition Check Body Update Condition Check.
Incorrect! Try again.
26Which keyword is used to return a value from a function?
A.break
B.exit
C.return
D.back
Correct Answer: return
Explanation:The return statement terminates the execution of a function and returns control (and an optional value) to the calling function.
Incorrect! Try again.
27Which of the following is true about structured programming?
A.It relies heavily on goto statements.
B.It emphasizes dividing a program into smaller functions or modules.
C.It does not support loops.
D.It is obsolete in C.
Correct Answer: It emphasizes dividing a program into smaller functions or modules.
Explanation:Structured programming discourages unconditional jumps (goto) and encourages block structures, loops, and modularization.
Incorrect! Try again.
28What is the output of the following?
c
int i = 0;
do {
i++;
} while (i < 0);
printf("%d", i);
A.0
B.1
C.Infinite Loop
D.Compiler Error
Correct Answer: 1
Explanation:A do-while loop executes the body at least once. i becomes 1. Then the condition 1 < 0 is false, so the loop terminates.
Incorrect! Try again.
29How many bytes does a long double typically occupy in C (implementation dependent, but generally)?
A.4 bytes
B.8 bytes
C.At least 8, often 10, 12 or 16 bytes
D.2 bytes
Correct Answer: At least 8, often 10, 12 or 16 bytes
Explanation:long double provides more precision than double. While standard double is usually 8 bytes, long double is often 10, 12, or 16 bytes depending on the architecture.
Incorrect! Try again.
30Which symbol is used as the address-of operator in scanf?
A.*
B.&
C.#
D.@
Correct Answer: &
Explanation:The & operator retrieves the memory address of a variable, which scanf requires to store the input data.
Incorrect! Try again.
31Which of the following is a valid unformatted input function?
A.scanf()
B.printf()
C.getch()
D.fprintf()
Correct Answer: getch()
Explanation:getch() (often found in conio.h or emulated) reads a single character without waiting for the enter key or formatting. scanf is formatted.
Incorrect! Try again.
32In the expression if (a > b && a > c), which logical operator is used?
A.OR
B.AND
C.NOT
D.XOR
Correct Answer: AND
Explanation:&& represents the logical AND operator. The condition is true only if both operands are true.
Incorrect! Try again.
33What is the correct format specifier for a double variable in scanf?
A.%f
B.%lf
C.%d
D.%ld
Correct Answer: %lf
Explanation:In scanf, %f is for float and %lf is specifically for double. (Note: In printf, %f works for both due to promotion, but scanf is strict).
Incorrect! Try again.
34Which of the following code structures is equivalent to a while loop?
A.A for loop without initialization and update parts.
B.An if statement.
C.A switch statement.
D.A do-while loop with condition 0.
Correct Answer: A for loop without initialization and update parts.
Explanation:for(; condition; ) behaves exactly like while(condition).
Incorrect! Try again.
35What is the effect of the type modifier const?
A.It allows the variable to change constantly.
B.It prevents the variable's value from being modified after initialization.
C.It forces the variable to be stored in CPU registers.
D.It makes the variable global.
Correct Answer: It prevents the variable's value from being modified after initialization.
Explanation:const defines a variable as read-only.
Incorrect! Try again.
36Which statement correctly prints a literal percentage sign?
A.printf("%")
B.printf("\%")
C.printf("%%")
D.printf("\%\%")
Correct Answer: printf("%%")
Explanation:Since % is the format escape character in printf, it must be escaped by itself (%%) to print a literal symbol.
Incorrect! Try again.
37When using switch, the expression inside the switch parentheses must evaluate to:
A.An integer or enumeration.
B.A float or double.
C.A string.
D.A boolean only.
Correct Answer: An integer or enumeration.
Explanation:Switch expressions must result in an integral type (int, char, enum).
Incorrect! Try again.
38What is the output of printf("%d", 10/3);?
A.3.33
B.3
C.4
D.3.0
Correct Answer: 3
Explanation:Integer division truncates the decimal part. .
Incorrect! Try again.
39Which function reads a single character from the standard input?
A.puts()
B.getchar()
C.printf()
D.write()
Correct Answer: getchar()
Explanation:getchar() reads the next character from stdin.
Incorrect! Try again.
40What is implicit type conversion also known as?
A.Casting
B.Coercion
C.Promotion
D.Demotion
Correct Answer: Coercion
Explanation:Automatic conversion done by the compiler (e.g., int to float in a mixed expression) is called coercion.
Incorrect! Try again.
41Is goto considered good practice in structured programming?
A.Yes, it is essential.
B.No, it creates 'spaghetti code'.
C.Only in while loops.
D.Yes, for memory management.
Correct Answer: No, it creates 'spaghetti code'.
Explanation:goto makes code flow unstructured and difficult to trace/debug, violating structured programming principles.
Incorrect! Try again.
42If x is a float, what does if(x == 0.1) likely result in?
A.True, if x was assigned 0.1.
B.Compiler Error.
C.Unreliable/False due to floating point precision.
D.True always.
Correct Answer: Unreliable/False due to floating point precision.
Explanation:Floating point numbers are stored approximately. Comparing them for exact equality is dangerous. 0.1 is a double literal, x is float; precision differences often make them unequal.
Incorrect! Try again.
43Which format specifier outputs a hexadecimal integer?
A.%d
B.%o
C.%x
D.%h
Correct Answer: %x
Explanation:%x formats an integer as a lowercase hexadecimal number.
Incorrect! Try again.
44The continue statement cannot be used in:
A.for loop
B.while loop
C.do-while loop
D.switch case
Correct Answer: switch case
Explanation:continue applies only to loops. Using it in a switch (unless the switch is inside a loop) causes an error.
Incorrect! Try again.
45What happens if you use scanf("%s", str) but input a string longer than the array str?
A.It truncates the string.
B.It automatically resizes the array.
C.Buffer overflow occurs.
D.The compiler prevents it.
Correct Answer: Buffer overflow occurs.
Explanation:scanf with %s writes past the allocated memory if the input is too long, leading to a buffer overflow vulnerability.
Incorrect! Try again.
46What is the logic of if(!a) where a is an integer?
A.Execute if a is not zero.
B.Execute if a is zero.
C.Execute if a is negative.
D.Compiler error.
Correct Answer: Execute if a is zero.
Explanation:If a is 0 (false), !a becomes 1 (true). If a is non-zero (true), !a becomes 0 (false).
Incorrect! Try again.
47Which of the following is a valid for loop syntax?
A.for(int i=0, i<10, i++)
B.for(int i=0; i<10; i++)
C.for(int i=0: i<10: i++)
D.for int i=0; i<10; i++
Correct Answer: for(int i=0; i<10; i++)
Explanation:The parts of a for loop header are separated by semicolons ;.
Incorrect! Try again.
48In the statement printf("%*d", width, value);, what does the * do?
A.It is a multiplication operator.
B.It is a pointer.
C.It takes the field width from the argument list.
D.It creates a syntax error.
Correct Answer: It takes the field width from the argument list.
Explanation:The * in a format specifier allows dynamic field width (or precision) to be supplied as an integer argument preceding the value.
Incorrect! Try again.
49Which loop guarantees that the body is executed at least once?
A.while
B.for
C.do-while
D.None
Correct Answer: do-while
Explanation:Since the condition is checked after the body execution, do-while runs at least once.
Incorrect! Try again.
50What is the value of i after int i = (3.5 > 2.0) ? 10 : 20;?
A.10
B.20
C.3.5
D.2.0
Correct Answer: 10
Explanation:This uses the ternary operator. The condition is true, so the first value (10) is assigned.