Unit 2 - Practice Quiz

CSE101 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which of the following is the correct syntax for an if-else statement in C?

A. if condition { // code } else { // code }
B. if (condition) { // code } else { // code }
C. if condition then { // code } else { // code }
D. if (condition) { // code } else (condition) { // code }

2 What 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

3 Which 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

4 What 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.

5 Which of the following is an exit-controlled loop?

A. for loop
B. while loop
C. do-while loop
D. if-else

6 What 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

7 Which 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

8 In 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.

9 Which format specifier is used for reading a single character in scanf?

A. %s
B. %d
C. %c
D. %f

10 What is the return type of the printf() function?

A. void
B. int
C. char
D. float

11 Consider the expression: What is the value of ?

A. 2.5
B. 2.0
C. 3.0
D. 5.0

12 Which type modifier allows a variable to hold only positive values?

A. short
B. long
C. signed
D. unsigned

13 Which function is safer to read a string containing spaces?

A. scanf("%s", str)
B. gets(str)
C. fgets(str, size, stdin)
D. getchar()

14 What 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.

15 What 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

16 What is the output of printf("%5.2f", 3.14159);?

A. 3.14159
B. 3.14
C. 3.14
D. 03.14

17 Which loop is best suited when the number of iterations is known beforehand?

A. while
B. do-while
C. for
D. goto

18 What does the goto statement require to function?

A. A loop
B. A label
C. A function call
D. A condition

19 The 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.

20 What is the result of the expression 5 % 2?

A. 2.5
B. 2
C. 1
D. 0

21 Which escape sequence is used to print a new line?

A. \t
B. \n
C. \b
D. \a

22 Which of the following creates an infinite loop?

A. for(;;)
B. while(1)
C. do { } while(1);
D. All of the above

23 What is the return type of scanf()?

A. void
B. float
C. int
D. char *

24 Which function prints a string followed automatically by a newline to stdout?

A. printf()
B. puts()
C. putchar()
D. fputs()

25 In 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.

26 Which keyword is used to return a value from a function?

A. break
B. exit
C. return
D. back

27 Which 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.

28 What 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

29 How 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

30 Which symbol is used as the address-of operator in scanf?

A. *
B. &
C. #
D. @

31 Which of the following is a valid unformatted input function?

A. scanf()
B. printf()
C. getch()
D. fprintf()

32 In the expression if (a > b && a > c), which logical operator is used?

A. OR
B. AND
C. NOT
D. XOR

33 What is the correct format specifier for a double variable in scanf?

A. %f
B. %lf
C. %d
D. %ld

34 Which 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.

35 What 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.

36 Which statement correctly prints a literal percentage sign?

A. printf("%")
B. printf("\%")
C. printf("%%")
D. printf("\%\%")

37 When 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.

38 What is the output of printf("%d", 10/3);?

A. 3.33
B. 3
C. 4
D. 3.0

39 Which function reads a single character from the standard input?

A. puts()
B. getchar()
C. printf()
D. write()

40 What is implicit type conversion also known as?

A. Casting
B. Coercion
C. Promotion
D. Demotion

41 Is 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.

42 If 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.

43 Which format specifier outputs a hexadecimal integer?

A. %d
B. %o
C. %x
D. %h

44 The continue statement cannot be used in:

A. for loop
B. while loop
C. do-while loop
D. switch case

45 What 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.

46 What 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.

47 Which 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++

48 In 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.

49 Which loop guarantees that the body is executed at least once?

A. while
B. for
C. do-while
D. None

50 What is the value of i after int i = (3.5 > 2.0) ? 10 : 20;?

A. 10
B. 20
C. 3.5
D. 2.0