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. while
B. if
C. for
D. switch

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. Neither block
B. Both blocks
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. default
C. otherwise
D. no_match

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

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

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

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

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

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

7 What is the purpose of the break statement?

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

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. next
B. skip
C. break
D. continue

9 What does the goto statement do in C?

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

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

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

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 Casting
B. Type Initialization
C. Type Declaration
D. Type Modification

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

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

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

Designing structured programs in C Easy
A. Using goto statements extensively
B. Using only global variables
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. gets()
B. printf()
C. puts()
D. scanf()

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. scanf()
B. gets()
C. printf()
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 floating-point number
B. A string
C. A single character
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. puts()
B. printf()
C. gets()
D. putchar()

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. getchar()
B. gets()
C. printf()
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 specifies a decimal number
B. It is a comment symbol
C. It represents a logical AND operation
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. After each iteration of the loop body
B. Before the first iteration
C. Only when the condition is false
D. Before the condition is checked

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. No output
D. Outer

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. Compilation Error
C. BC
D. BCD

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. -54.3
B. 16.7
C. Compilation Error
D. -49.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. The address of a
B. 3
C. 2
D. A non-zero value indicating success

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. No output
C. Compilation Error
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 4 5
B. 1 2 3
C. 1 2 4 5
D. 1 2

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.35 |
C. | 12.35|
D. | 12.34|

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. 2 3 4
B. 1 2 3 4 5
C. 2 3 4 5
D. 1 2 3 4

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. Compilation Error
B. Greater
C. Default
D. Not Greater

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

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

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) (1,0)
C. (0,0) (0,1) (0,2)
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. Compilation Error
C. 2.0
D. 0.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. 0 1 2
B. 5 5 5
C. 5 6 7
D. 5 4 3

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 does not perform bounds checking, which can lead to buffer overflow vulnerabilities.
B. It is significantly less efficient than fgets() or scanf().
C. It requires a special library to be included that is not part of the C standard.
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. True
B. False
C. No output
D. Compilation Error

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. 0 1 2 Done
B. This will result in an infinite loop.
C. 0 1 2
D. Done

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=' ', c3='B'
B. c1=' ', c2='A', c3=' '
C. c1='A', c2='B', c3='\n'
D. The program will wait for more input.

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 = 'P'; } else { result = 'F'; }
B. c
if (score >= 60) { result = 'F'; } else { result = 'P'; }
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 = 6, y = 11, z = 16
B. x = 6, y = 12, z = 14
C. x = 6, y = 11, z = 15
D. x = 5, y = 11, z = 15

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. Compilation Error
B. Hello
Case 1
C. Undefined Behavior
D. Case 1

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. 8 times
B. 15 times
C. The loop is infinite
D. 9 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=2, c='h'
B. ret=3, c='h'
C. ret=2, c=' '
D. ret=3, c=' '

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. x is greater
D. Compilation Error

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
B. Infinite loop
C. 1 2
D. 1 3

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. 6
B. 12
C. 3
D. 9

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 = 0
C. Compilation Error
D. Value is 12345
n = 12345

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. Prints a garbage value
B. Prints 10
C. Compilation Error
D. Undefined Behavior

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 will always work correctly and print 100.
B. It will cause a memory leak because local_var is not freed.
C. It returns a pointer to a local variable, which leads to undefined behavior upon dereferencing.
D. It will cause a compilation error because a pointer cannot be returned from a function.

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 prints "20".
B. The program prints "Case 2" as is, but with the line uncommented, it still prints "Case 2".
C. The program causes a compilation error as is.
D. The program prints "Case 2" as is, but with the line uncommented, it causes a compilation error.

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 is an infinite loop.
C. It executes a number of times equal to the number of set bits (1s) in the binary representation of n.
D. It executes log2(n) times.

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. B
B. A
C. D
D. C

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
B. CS
C. CS-101 is fun
D. CS-101

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. Pass the struct by value: void modify_struct(LargeStruct s)
B. Make the struct a global variable
C. Return the modified struct: LargeStruct 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,9,8,
B. 10,11,12,
C. 10,10,10,
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. True
C. Compilation Error
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 behavior is undefined.
B. The code prints 20.
C. The code prints 10.
D. The code results in a compilation error.

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 5
B. 1 2 3 4 5
C. 1 3 5 7
D. 1 3