Unit 2 - Practice Quiz

CSE101 59 Questions
0 Correct 0 Wrong 59 Left
0/59

1 Which of the following keywords is used to make a decision based on a condition in C?

If Easy
A. switch
B. for
C. while
D. if

2 In an if-else statement, which block of code is executed if the condition in the if part is false?

If else Easy
A. Both blocks
B. Neither block
C. The if block
D. The else block

3 In a switch statement, which keyword is used to specify the code to run if no case constant matches the expression?

Switch case statements Easy
A. else
B. no_match
C. default
D. otherwise

4 Which loop is generally preferred when the number of iterations is known in advance?

For Easy
A. do-while loop
B. while loop
C. for loop
D. infinite loop

5 What is a key feature of the do-while loop?

Do-while loops Easy
A. It checks the condition before the first iteration.
B. It cannot be an infinite loop.
C. It is only used for a fixed number of iterations.
D. It executes the loop body at least once.

6 What is a loop that continues to execute endlessly called?

While Easy
A. Nested Loop
B. Final Loop
C. Conditional Loop
D. Infinite Loop

7 What is the purpose of the break statement?

Break and continue statements Easy
A. To terminate the entire program
B. To skip the current iteration and continue with the next
C. To pause the program's execution
D. To exit immediately from a loop or switch statement

8 Which keyword is used to skip the rest of the current loop iteration and start the next one?

Break and continue statements Easy
A. skip
B. break
C. next
D. continue

9 What does the goto statement do in C?

Goto Easy
A. Calls a function
B. Returns a value from a function
C. Transfers control to a labeled statement within the same function
D. Declares a global variable

10 What is the primary purpose of the return statement in a C function?

Return Easy
A. To terminate a function's execution and optionally send a value back to the caller
B. To stop the execution of the entire program
C. To start the execution of a function
D. To print a value to the console

11 The process of converting one data type (e.g., int) to another (e.g., float) is known as:

Type conversion and type modifiers Easy
A. Type Modification
B. Type Initialization
C. Type Declaration
D. Type Casting

12 Which of the following is a type modifier in C?

Type conversion and type modifiers Easy
A. if
B. return
C. long
D. float

13 Which of the following is a fundamental concept of structured programming?

Designing structured programs in C Easy
A. Using only global variables
B. Using goto statements extensively
C. Writing the entire code in the main() function
D. Breaking down a program into smaller, manageable functions or modules

14 Which C standard library function is used to send formatted output to the standard output (usually the screen)?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Easy
A. scanf()
B. printf()
C. puts()
D. gets()

15 Which C function is used to read formatted input from the standard input (usually the keyboard)?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Easy
A. printf()
B. scanf()
C. gets()
D. puts()

16 In C, what type of data does the format specifier %d correspond to in functions like printf() and scanf()?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Easy
A. A string
B. A single character
C. A floating-point number
D. A signed decimal integer

17 Which unformatted I/O function is used to write a string to the output and automatically appends a newline character (\n)?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Easy
A. putchar()
B. gets()
C. printf()
D. puts()

18 Which function is generally used to read a line of text, including spaces, from the standard input?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Easy
A. gets()
B. printf()
C. getchar()
D. scanf("%s", ...)

19 In the statement scanf("%d", &num);, what is the purpose of the ampersand (&) symbol?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Easy
A. It is a comment symbol
B. It represents a logical AND operation
C. It specifies a decimal number
D. It is the 'address of' operator, providing the memory location of the variable num

20 In a for loop with the structure for(initialization; condition; update), when is the update part executed?

For Easy
A. Before the condition is checked
B. After each iteration of the loop body
C. Before the first iteration
D. Only when the condition is false

21 What is the output of the following C code snippet?

c
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 5; i++);
{
printf("%d ", i);
}
return 0;
}

For Medium
A. 0 1 2 3 4 5
B. 5
C. Compilation Error
D. 0 1 2 3 4

22 Predict the output of the following C code, which demonstrates the 'dangling else' problem.

c
#include <stdio.h>

int main() {
int a = 10, b = 20;
if (a > 5)
if (b > 25)
printf("Inner");
else
printf("Outer");
return 0;
}

If else Medium
A. Inner
B. InnerOuter
C. Outer
D. No output

23 What is the output of this C code due to the 'fall-through' behavior in the switch statement?

c
#include <stdio.h>

int main() {
int x = 2;
switch (x) {
case 1: printf("A");
case 2: printf("B");
case 3: printf("C"); break;
default: printf("D");
}
return 0;
}

Switch case statements Medium
A. B
B. BC
C. BCD
D. Compilation Error

24 What will be printed by the following C code snippet after evaluating the expression with mixed data types?

c
#include <stdio.h>

int main() {
float f = 5.7;
int i = 10;
char c = 'A'; // ASCII value of 'A' is 65
double result = f + i / 2.0 - c;
printf("%.1f", result);
return 0;
}

Type conversion and type modifiers Medium
A. 16.7
B. -49.3
C. Compilation Error
D. -54.3

25 Consider the following code. If the user provides the input 10 20.5, what will be the final value of the variable ret?

c
#include <stdio.h>

int main() {
int a, ret;
float b;
ret = scanf("%d %f", &a, &b);
return 0;
}

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Medium
A. 2
B. The address of a
C. A non-zero value indicating success
D. 3

26 What is the output of the following C code snippet?

c
#include <stdio.h>

int main() {
int i = 5;
do {
printf("In ");
} while (i < 5);
return 0;
}

Do-while loops Medium
A. In In In In In
B. Compilation Error
C. No output
D. In

27 What is the output of the following C code that uses a continue statement inside a loop?

c
#include <stdio.h>

int main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
return 0;
}

Break and continue statements Medium
A. 1 2 3
B. 1 2
C. 1 2 4 5
D. 1 2 3 4 5

28 What is the precise output of the C statement printf("|%6.2f|", 12.345);?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Medium
A. |12.345|
B. | 12.34|
C. | 12.35|
D. |12.35 |

29 Predict the output of the given C code snippet, paying close attention to the post-increment operator.

c
#include <stdio.h>

int main() {
int x = 1;
while (x++ < 5) {
printf("%d ", x);
}
return 0;
}

While Medium
A. 1 2 3 4 5
B. 1 2 3 4
C. 2 3 4
D. 2 3 4 5

30 What is the output of the following C code where a conditional expression is used inside a switch statement?

c
#include <stdio.h>

int main() {
int i = 5;
switch (i > 5) {
case 1: printf("Greater"); break;
case 0: printf("Not Greater"); break;
default: printf("Default");
}
return 0;
}

Switch case statements Medium
A. Greater
B. Not Greater
C. Compilation Error
D. Default

31 In a C program defined as int main(), what is the primary significance of the statement return 0;?

Return Medium
A. It indicates that the program executed successfully to the operating system.
B. It returns the integer value 0 to the calling function within the same program.
C. It is a mandatory statement to stop the execution of any C function.
D. It clears all memory allocated by the program before termination.

32 What is the output of this C code containing a break statement inside a nested loop?

c
#include <stdio.h>

int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break;
}
printf("(%d,%d) ", i, j);
}
}
return 0;
}

Break and continue statements Medium
A. (0,0) (0,1) (0,2) (1,0) (2,0) (2,1) (2,2)
B. (0,0) (0,1) (0,2)
C. (0,0) (0,1) (0,2) (1,0)
D. (0,0)

33 What is the output of the following C code snippet that uses explicit type casting?

c
#include <stdio.h>

int main() {
int a = 5, b = 2;
float result = (float)a / b;
printf("%.1f", result);
return 0;
}

Type conversion and type modifiers Medium
A. 2.5
B. 0.0
C. Compilation Error
D. 2.0

34 What is the output of the following C code, which uses multiple expressions in its for loop definition?

c
#include <stdio.h>

int main() {
int i, j;
for (i = 0, j = 5; i < j; i++, j--) {
printf("%d ", i + j);
}
return 0;
}

For Medium
A. 5 5 5
B. 5 4 3
C. 0 1 2
D. 5 6 7

35 Why is the gets() function considered unsafe and generally avoided in modern C programming?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Medium
A. It requires a special library to be included that is not part of the C standard.
B. It is significantly less efficient than fgets() or scanf().
C. It does not perform bounds checking, which can lead to buffer overflow vulnerabilities.
D. It cannot read strings containing whitespace characters.

36 What will be the output of the following C code snippet due to the operator used in the if condition?

c
#include <stdio.h>

int main() {
int x = 0;
if (x = 0) {
printf("False");
} else {
printf("True");
}
return 0;
}

If Medium
A. No output
B. False
C. Compilation Error
D. True

37 Analyze the control flow of this C program. What will be its final output?

c
#include <stdio.h>

int main() {
int i = 0;
loop_start:
if (i >= 3) {
goto end;
}
printf("%d ", i);
i++;
goto loop_start;

end:
printf("Done");
return 0;
}

Goto Medium
A. Done
B. 0 1 2
C. 0 1 2 Done
D. This will result in an infinite loop.

38 If a user enters A B (note the leading and internal spaces) for the C code char c1, c2, c3; scanf("%c%c%c", &c1, &c2, &c3);, what values will be stored in the variables?

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Medium
A. c1='A', c2='B', c3='\n'
B. c1='A', c2=' ', c3='B'
C. The program will wait for more input.
D. c1=' ', c2='A', c3=' '

39 Which of the following practices is a core principle of structured programming, designed to improve code clarity, reusability, and maintainability?

Designing structured programs in C Medium
A. Writing the entire program within a single, large main function to keep it in one place.
B. Using goto statements as the primary method for controlling program flow.
C. Decomposing a large problem into smaller, single-purpose, and self-contained functions or modules.
D. Placing all program logic and variables in the global scope for easy access.

40 Which of the following if-else blocks is functionally equivalent to the C statement result = (score >= 60) ? 'P' : 'F';?

If else Medium
A. c
if (score >= 60) { result = 'F'; } else { result = 'P'; }
B. c
if (score >= 60) { result = 'P'; } else { result = 'F'; }
C. c
if (score < 60) { result = 'P'; } else { result = 'F'; }
D. c
if (score = 60) { result = 'P'; } else { result = 'F'; }

41 Analyze the following C code snippet. What will be the final values of x, y, and z?

c
int x = 5, y = 10, z = 15;
if (x++ > 5 && ++y > 10 || z-- > 15) {
y++;
}
else {
z++;
}

If else Hard
A. x = 5, y = 11, z = 15
B. x = 6, y = 12, z = 14
C. x = 6, y = 11, z = 15
D. x = 6, y = 11, z = 16

42 What is the output of the following C program?

c
#include <stdio.h>
int main() {
int i = 1;
switch (i) {
printf("Hello\n");
case 1:
printf("Case 1\n");
break;
case 2:
printf("Case 2\n");
break;
default:
printf("Default\n");
}
return 0;
}

Switch case statements Hard
A. Hello
Case 1
B. Case 1
C. Compilation Error
D. Undefined Behavior

43 How many times will the printf function be executed in the following C code snippet?

c
#include <stdio.h>
int main() {
int i, j, count = 0;
for (i = 5; i > 0; i = i - 2) {
for (j = i; j > 0; j--) {
printf("*");
}
}
return 0;
}

For loops Hard
A. 9 times
B. 15 times
C. The loop is infinite
D. 8 times

44 Consider the following C code. If the user provides the input 123 45.67 hello, what will be the value returned by scanf and what will be the final value of c?

c
#include <stdio.h>
int main() {
int a, ret;
float b;
char c;
ret = scanf("%d%f %c", &a, &b, &c);
printf("ret=%d, c=%c\n", ret, c);
return 0;
}

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Hard
A. ret=3, c=' '
B. ret=2, c='h'
C. ret=2, c=' '
D. ret=3, c='h'

45 What is the output of this C code snippet? Assume int is 32 bits.

c
#include <stdio.h>
int main() {
int x = -1;
unsigned int y = 1;
if (x > y) {
printf("x is greater");
}
else {
printf("y is greater
");
}
return 0;
}

Type conversion and type modifiers Hard
A. y is greater
B. Undefined Behavior
C. Compilation Error
D. x is greater

46 Predict the output of the following C program.

c
#include <stdio.h>
int main() {
int i = 0;
do {
i++;
if (i == 2)
continue;
printf("%d ", i);
} while (i < 2);
return 0;
}

Do-while loops Hard
A. 1 2
B. 1 3
C. Infinite loop
D. 1

47 What will be the final value of sum after this code executes?

c
int i, j, sum = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (j == i) {
break;
}
sum += j;
}
sum += i;
}

Break and continue statements Hard
A. 9
B. 6
C. 3
D. 12

48 What is the most likely output of the following C code?

c
#include <stdio.h>
int main() {
int n = 0;
printf("Value is %d%n\n", 12345, &n);
printf("n = %d\n", n);
return 0;
}

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Hard
A. Value is 12345
n = 5
B. Value is 12345
n = 12345
C. Value is 12345
n = 0
D. Compilation Error

49 What is the behavior of the following C code snippet?

c
#include <stdio.h>
int main() {
goto label;
int x = 10; // Initialization
label:
printf("%d\n", x);
return 0;
}

Goto Hard
A. Compilation Error
B. Prints 10
C. Undefined Behavior
D. Prints a garbage value

50 What is the primary issue with the following C function?

c
int create_and_init() {
int local_var = 100;
int
ptr = &local_var;
return ptr;
}

int main() {
int my_ptr = create_and_init();
printf("%d\n",
my_ptr);
return 0;
}

Return Hard
A. It returns a pointer to a local variable, which leads to undefined behavior upon dereferencing.
B. It will cause a memory leak because local_var is not freed.
C. It will cause a compilation error because a pointer cannot be returned from a function.
D. It will always work correctly and print 100.

51 What is the output of this C code snippet?

c
#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1:
int y = 10;
printf("%d", y);
break;
case 2:
// y = 20; // This line would cause a compile error
printf("Case 2");
break;
}
return 0;
}

What happens if the commented-out line y = 20; is uncommented?

Switch case statements Hard
A. The program prints "Case 2" as is, but with the line uncommented, it causes a compilation error.
B. The program causes a compilation error as is.
C. The program prints "Case 2" as is, but with the line uncommented, it still prints "Case 2".
D. The program prints "Case 2" as is, but with the line uncommented, it prints "20".

52 For a non-zero integer n, how many times does the following while loop execute?

c
int count = 0;
while (n) {
n = n & (n - 1);
count++;
}

While loops Hard
A. It executes n times.
B. It executes log2(n) times.
C. It executes a number of times equal to the number of set bits (1s) in the binary representation of n.
D. It is an infinite loop.

53 What is the behavior of this C code? Assume a little-endian architecture where int is 4 bytes.

c
#include <stdio.h>
int main() {
int num = 0x41424344; // Hex for 'ABCD'
char c_ptr = (char)#
printf("%c\n", *(c_ptr + 1));
return 0;
}

Type conversion and type modifiers Hard
A. D
B. A
C. C
D. B

54 What is the final value of the string str after the scanf call, given the input CS-101 is fun?

c
#include <stdio.h>
int main() {
char str[50];
scanf(" %[^-\n]s", str);
printf("%s", str);
return 0;
}

Formatted and unformatted Input/Output functions like printf(), Scanf(), Puts(), Gets() etc Hard
A. CS-101
B. CS-101 is fun
C. CS
D. CS

55 A C function needs to modify a large struct variable passed to it from the caller. From a performance and design perspective, what is the best way to pass this struct to the function?

c
typedef struct {
double data[1000];
int id;
} LargeStruct;

// How should we declare modify_struct?
void modify_struct(?);

Designing structured programs in C Hard
A. Make the struct a global variable
B. Return the modified struct: LargeStruct modify_struct(LargeStruct s)
C. Pass the struct by value: void modify_struct(LargeStruct s)
D. Pass a pointer to the struct: void modify_struct(LargeStruct* s)

56 Predict the output of the following C code, which uses the comma operator extensively.

c
#include <stdio.h>
int main() {
int i = 0, j = 10;
for (i = 0, j = 10; i < j; ++i, j-=2) {
printf("%d,", i + j);
}
return 0;
}

For loops Hard
A. 10,10,10,
B. 10,11,12,
C. 10,9,8,
D. 10,10,10,10,

57 What is the output of this C code, assuming sizeof(char) is 1 and sizeof(int) is 4?

c
#include <stdio.h>
int main() {
if (sizeof(int) > -1) {
printf("True");
}
else {
printf("False");
}
return 0;
}

If else Hard
A. False
B. Compilation Error
C. True
D. Undefined Behavior

58 What is the behavior of the following C code?

c
void modify(const int ptr) {
int
non_const_ptr = (int)ptr;
non_const_ptr = 20;
}

int main() {
const int x = 10;
modify(&x);
printf("%d\n", x);
return 0;
}

Type conversion and type modifiers Hard
A. The code results in a compilation error.
B. The code prints 10.
C. The code prints 20.
D. The behavior is undefined.

59 What is the output of the following C code?

c
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
if (i > 5) {
break;
}
printf("%d ", i);
}
return 0;
}

Break and continue statements Hard
A. 1 3
B. 1 2 3 4 5
C. 1 3 5
D. 1 3 5 7