Unit 3 - Practice Quiz

CSE101 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is the primary purpose of a function prototype in C?

Function prototypes Easy
A. To declare the function's name, return type, and parameters to the compiler before it is defined.
B. To call the function from the main() function.
C. To define the actual logic of the function.
D. To store the return value of a function.

2 What are the two main parts of a function definition?

Function definition Easy
A. The function prototype and the function arguments
B. The function header and the function body
C. The function call and the return statement
D. The return type and the function name

3 When you pass an argument by value, what is actually passed to the function?

Function call including passing arguments by value and passing arguments by reference Easy
A. A copy of the argument's value
B. The memory address of the argument
C. The original argument itself
D. A pointer to the argument

4 A function that calls itself is known as a ____.

Recursive functions Easy
A. nested function
B. static function
C. recursive function
D. library function

5 A variable declared inside a function is known as a ____.

Scope rules (local and global scope) Easy
A. global variable
B. external variable
C. local variable
D. static variable

6 Which storage class is the default for local variables declared inside a function?

Storage classes in C namely auto, Extern, Register, Static storage classes Easy
A. register
B. auto
C. static
D. extern

7 Which header file is required to use the sqrt() function in C?

Math library functions Easy
A. <string.h>
B. <stdio.h>
C. <stdlib.h>
D. <math.h>

8 How is 'pass by reference' typically simulated in the C programming language?

Function call including passing arguments by value and passing arguments by reference Easy
A. By passing pointers (memory addresses) as arguments
B. C does not support pass by reference
C. By using global variables
D. By using the & symbol in the function definition

9 What is the main characteristic of a static local variable?

Storage classes in C namely auto, Extern, Register, Static storage classes Easy
A. It can only be an integer.
B. It is stored in a CPU register.
C. It retains its value between function calls.
D. It is automatically deleted after the function call.

10 Where can a global variable be accessed from?

Scope rules (local and global scope) Easy
A. Only from the function where it is declared
B. Only from functions within the same file
C. Only from the main() function
D. From any function in the program

11 What is the condition that stops a recursive function from calling itself infinitely?

Recursive functions Easy
A. Base case
B. An exit() function call
C. Recursive case
D. A break statement

12 What does the extern keyword signify for a variable declaration?

Storage classes in C namely auto, Extern, Register, Static storage classes Easy
A. The variable's value is fixed.
B. The variable is external to all functions.
C. The variable should be stored outside of the computer's memory.
D. The variable is defined in another source file or later in the same file.

13 What will be the result of the C expression pow(3.0, 2.0)?

Math library functions Easy
A. 8.0
B. 9.0
C. 1.5
D. 6.0

14 A function prototype in C must always end with which character?

Function prototypes Easy
A. : (colon)
B. ; (semicolon)
C. . (period)
D. } (closing curly brace)

15 The part of a function definition that contains the executable statements is called the ____.

Function definition Easy
A. function body
B. function header
C. function prototype
D. return statement

16 If a function is defined as void func(int x), what kind of parameter passing does it use?

Function call including passing arguments by value and passing arguments by reference Easy
A. Pass by value
B. Pass by pointer
C. Pass by address
D. Pass by reference

17 The register storage class is a hint to the compiler to store a variable in ____.

Storage classes in C namely auto, Extern, Register, Static storage classes Easy
A. main memory (RAM)
B. a file on the disk
C. a CPU register
D. static memory

18 What happens if a local variable is declared with the same name as a global variable inside a function?

Scope rules (local and global scope) Easy
A. The program will result in a compilation error.
B. The local variable takes precedence inside that function.
C. The program will crash at runtime.
D. The global variable takes precedence inside that function.

19 Which function from <math.h> would you use to find the absolute value of a floating-point number?

Math library functions Easy
A. absval()
B. abs()
C. sqrt()
D. fabs()

20 In the function call calculate(&num);, what does the & operator do?

Function call including passing arguments by value and passing arguments by reference Easy
A. It doubles the value of the variable num.
B. It creates a reference to the variable num.
C. It provides the memory address of the variable num.
D. It provides the value of the variable num.

21 What is the primary consequence of calling a function before its definition appears in the source file, without providing a function prototype?

Function prototypes Medium
A. A runtime error will occur immediately upon calling the function.
B. The program will compile with a warning, and the compiler will assume a default prototype int function_name(), which may lead to runtime errors.
C. A linker error will occur because the function object code cannot be found.
D. The program will not compile, citing a syntax error.

22 Analyze the following C code. What will be its output?
c
#include <stdio.h>

void swap(int x, int y) {
int temp =
x;
*x = y;
y = temp;
}

int main() {
int a = 10, b = 20;
swap(&a, b);
printf("a = %d, b = %d\n", a, b);
return 0;
}

Function call including passing arguments by value and passing arguments by reference Medium
A. a = 20, b = 10
B. a = 10, b = 20
C. a = 20, b = 20
D. Compiler Error

23 What will be the output of the following C program?
c
#include <stdio.h>

int value = 5;

void display(int value) {
value = 20;
printf("%d ", value);
}

int main() {
printf("%d ", value);
display(10);
printf("%d\n", value);
return 0;
}

Scope rules (local and global scope) Medium
A. 5 20 20
B. 5 10 10
C. 5 20 5
D. 5 10 5

24 What is the output of the following code when it is executed?
c
#include <stdio.h>

void count_calls() {
static int counter = 1;
printf("%d ", counter);
counter += 5;
}

int main() {
count_calls();
count_calls();
count_calls();
return 0;
}

Static storage classes Medium
A. Compiler Error
B. 1 6 11
C. 1 5 10
D. 1 1 1

25 Consider the following recursive function. What does calculate(4) return?
c
int calculate(int n) {
if (n <= 1) {
return 1;
}
return n + calculate(n - 2);
}

Recursive functions Medium
A. 8
B. 7
C. 10
D. 6

26 Suppose you have two C files, main.c and support.c. What is the most likely result of compiling and linking them?

support.c
c
int global_var = 100;


main.c
c
#include <stdio.h>

int global_var;

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

Extern storage classes Medium
A. The program compiles and prints 100.
B. The program compiles and prints 0.
C. A linker error due to multiple definitions of global_var.
D. A compiler error in main.c.

27 What is the output of the following C code snippet? (Assume math.h is included).
c
#include <stdio.h>
#include <math.h>

int main() {
double result = pow(2, 3) + ceil(4.01) - floor(5.99);
printf("%.1f\n", result);
return 0;
}

Math library functions Medium
A. 8.0
B. 9.0
C. 8.2
D. 7.0

28 What will be printed by the following C code? Note how arrays are passed to functions.
c
#include <stdio.h>

void modify_array(int arr[], int size) {
arr[0] = 100;
size = 10;
}

int main() {
int numbers[] = {1, 2, 3, 4, 5};
int n = 5;
modify_array(numbers, n);
printf("numbers[0] = %d, n = %d\n", numbers[0], n);
return 0;
}

Function call including passing arguments by value and passing arguments by reference Medium
A. numbers[0] = 100, n = 10
B. numbers[0] = 1, n = 5
C. numbers[0] = 100, n = 5
D. numbers[0] = 1, n = 10

29 Which of the following operations is invalid for a variable declared with the register storage class?

Register storage classes Medium
A. Initializing it at the time of declaration.
B. Passing it by value to another function.
C. Getting its memory address using the & operator.
D. Using it as a loop counter.

30 What is the output of the following code for the call fun(25)?
c
#include <stdio.h>

void fun(int n) {
if (n == 0)
return;
fun(n / 2);
printf("%d", n % 2);
}

Recursive functions Medium
A. 10011
B. The function runs indefinitely.
C. 25
D. 11001

31 Predict the output of the given C code.
c
#include <stdio.h>

int x = 10;

int main() {
int x = 20;
{
extern int x;
printf("%d ", x);
}
printf("%d\n", x);
return 0;
}

Scope rules (local and global scope) Medium
A. Compiler Error
B. 10 10
C. 10 20
D. 20 20

32 Consider the following C code. What is the most likely outcome?
c
#include <stdio.h>

// Prototype
void display(int);

int main() {
display(5.5);
return 0;
}

// Definition
void display(int n) {
printf("n = %d\n", n);
}

Function definition Medium
A. The code compiles and prints "n = 6".
B. The code fails to compile due to a type mismatch in the function call.
C. The code compiles and prints "n = 5".
D. The code compiles but results in a runtime error.

33 What is the primary difference between a static global variable and a non-static (regular) global variable in C?

Static storage classes Medium
A. A static global variable has internal linkage, limiting its visibility to the translation unit (the file) in which it is defined.
B. A static global variable cannot be modified after initialization.
C. A static global variable is stored on the stack, while a regular global is stored in the data segment.
D. A static global variable retains its value across program executions, while a regular global does not.

34 Examine the following C code. What will be the output?
c
#include <stdio.h>

void update(int *p) {
int y = 20;
p = &y;
}

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

Function call including passing arguments by value and passing arguments by reference Medium
A. Compiler Error
B. Undefined behavior (printing a value from a dangling pointer).
C. 20
D. 10

35 Which of the following C statements correctly and safely calculates for a double variable x and stores it in a double variable y? (Assume math.h is included).

Math library functions Medium
A. y = |pow(x, 3.0)|;
B. y = pow(abs(x), 3.0);
C. y = fabs(pow(x, 3.0));
D. y = abs(pow(x, 3.0));

36 What are the default storage class and initial value of a variable declared inside a function without any storage class specifier, like int count;?

Auto storage classes Medium
A. static storage class, and its initial value is 0.
B. auto storage class, and its initial value is 0.
C. register storage class, and its initial value is indeterminate (garbage).
D. auto storage class, and its initial value is indeterminate (garbage).

37 If a function is defined as float calculate(float a, int b);, which of the following is NOT a valid prototype for it?

Function prototypes Medium
A. float calculate();
B. float calculate(float, int);
C. float calculate(float value, int count);
D. extern float calculate(float a, int b);

38 What is a potential major drawback of the following recursive implementation for calculating Fibonacci numbers compared to an iterative approach?
c
int fib(int n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}

Recursive functions Medium
A. It has exponential time complexity due to recalculating the same values multiple times.
B. It cannot calculate Fibonacci numbers for even values of n.
C. It will always result in a stack overflow for n > 2.
D. The base case is incorrect, leading to an infinite recursion.

39 What is the output of the following C code?
c
#include <stdio.h>

int generator() {
static int i = 0;
i += 2;
return i;
}

int main() {
int a = generator();
int b = generator();
printf("%d %d\n", a, b);
return 0;
}

Static storage classes Medium
A. 0 2
B. 2 2
C. 2 4
D. 4 4

40 Which of the following statements is true regarding the initialization of a variable declared with extern?

Extern storage classes Medium
A. A variable declared with extern cannot be initialized because it is only a declaration, not a definition.
B. An extern variable is always initialized to 0 by the compiler.
C. An extern variable inherits the value of the last variable declared in the previous scope.
D. An extern variable must be initialized at the point of its declaration.

41 Analyze the following C code that uses a static local variable within a recursive function. What will be printed to standard output?

c
#include <stdio.h>

int recursive_static(int n) {
static int s = 5;
if (n <= 1) {
return s;
}
s += n;
recursive_static(n - 1);
return s;
}

int main() {
printf("%d", recursive_static(4));
return 0;
}

Static storage classes Hard
A. 5
B. 14
C. 16
D. 12

42 What is the output of the following C program that manipulates pointers to pointers?

c
#include <stdio.h>

void swap_ptrs(int ppa, int ppb) {
int temp = ppa;
ppa = 100;
ppa = ppb;
ppb = 200;
ppb = temp;
}

int main() {
int a = 10, b = 20;
int
pa = &a, pb = &b;
swap_ptrs(&pa, &pb);
printf("a=%d, b=%d,
pa=%d, pb=%d", a, b, pa, *pb);
return 0;
}

Function call including passing arguments by value and passing arguments by reference Hard
A. a=100, b=20, pa=20, pb=100
B. a=200, b=10, pa=10, pb=200
C. a=100, b=200, pa=100, pb=200
D. a=10, b=20, pa=20, pb=10

43 Predict the output of the following C program, which demonstrates complex variable shadowing.

c
#include <stdio.h>

int x = 10;

void display(int x) {
x += 5;
{
int x = ::x + 5; // Note: ::x is not valid C syntax. Assuming it meant the global x.
// C does not have a scope resolution operator. This code is invalid.
// Let's modify the question to be valid C and still hard.
}
}

Let's re-design the question.

c
#include <stdio.h>
int val = 1;
int main() {
int val = 10;
{
extern int val;
printf("%d, ", val);
}
printf("%d", val);
return 0;
}

What is the behavior of this program?

Scope rules (local and global scope) Hard
A. Linker Error
B. 10, 10
C. 1, 1
D. 1, 10

44 Analyze the following mutually recursive functions. What is the final output when A(6) is called?

c
#include <stdio.h>

void B(int n);

void A(int n) {
if (n > 0) {
printf("%d ", n);
B(n - 1);
}
}

void B(int n) {
if (n > 1) {
printf("%d ", n);
A(n / 2);
}
}

int main() {
A(6);
return 0;
}

Recursive functions Hard
A. 6 5 4 3 2 1
B. 6 5 4 2 1
C. 6 5 2
D. 6 5 2 1

45 Consider two C files, f1.c and f2.c. What is the behavior when they are compiled and linked together?

f1.c:
c
#include <stdio.h>

int main() {
extern int global_var;
printf("%d", global_var);
return 0;
}


f2.c:
c
static int global_var = 100;

Extern storage class Hard
A. The program compiles and prints 0.
B. A linker error occurs.
C. The program compiles and prints 100.
D. A compilation error occurs in f1.c.

46 According to the C90 standard, what is the most likely result of compiling and running the following code where a function is called without a prior prototype?

c
#include <stdio.h>

int main() {
printf("%ld", sizeof(get_val()));
return 0;
}

long double get_val() {
return 100.0L;
}

Function prototypes Hard
A. It prints sizeof(long double), which is typically 10, 12, or 16.
B. It results in a compilation error.
C. It prints sizeof(int), which is typically 4.
D. It results in a runtime crash.

47 What is the output of the following C program that deals with edge cases of mathematical functions?

c
#include <stdio.h>
#include <math.h>

int main() {
double p1 = pow(0.0, 0.0);
double p2 = pow(-1.0, INFINITY);
double r = remainder(10.0, 3.0);
printf("%.1f %.1f %.1f", p1, p2, r);
return 0;
}

Math library functions Hard
A. nan 1.0 -2.0
B. 1.0 1.0 -2.0
C. 1.0 1.0 1.0
D. 1.0 nan 1.0

48 What is the primary reason the following C code fails to compile?

c
#include <stdio.h>

int main() {
register int reg_var = 10;
int ptr = ®_var;

ptr = 20;
printf("%d", reg_var);

return 0;
}

Register storage class Hard
A. A register variable is read-only after initialization.
B. The address-of operator (&) cannot be applied to a register variable.
C. A register variable cannot be initialized.
D. A pointer cannot point to a register variable.

49 The Ackermann function is a classic example of a recursive function that is not primitive recursive. What is the value of ack(2, 3)?

c
int ack(int m, int n) {
if (m == 0) {
return n + 1;
} else if (n == 0) {
return ack(m - 1, 1);
} else {
return ack(m - 1, ack(m, n - 1));
}
}

Recursive functions Hard
A. 9
B. 7
C. 11
D. Stack Overflow

50 What is printed by the following program, which passes an array to a function and uses the sizeof operator? (Assume a 64-bit system where sizeof(int) is 4 and sizeof(int*) is 8).

c
#include <stdio.h>

void process_array(int arr[20]) {
printf("%zu,", sizeof(arr));
}

int main() {
int data[20];
printf("%zu,", sizeof(data));
process_array(data);
return 0;
}

Function call including passing arguments by value and passing arguments by reference Hard
A. 80,8
B. 80,80
C. 8,8
D. 8,80

51 Analyze this code. A function returns a pointer to a static local variable. What is the output?

c
#include <stdio.h>

int get_counter() {
static int counter = 10;
counter += 5;
return &counter;
}

int main() {
int
p1 = get_counter();
int p2 = get_counter();
p1 += 5;
printf("%d", *p2);
return 0;
}

Static storage classes Hard
A. 15
B. 20
C. Undefined Behavior
D. 25

52 In C, a declaration extern int var = 10; is placed at file scope in a single file program.c. Which statement accurately describes this line?

Extern storage class Hard
A. It is a full definition, not just a declaration. It allocates storage for var and provides an initial value.
B. It is a declaration, and the program requires another file to provide the definition of var.
C. It is a syntax error because extern declarations cannot have initializers.
D. It is a tentative definition that will be initialized to 0 by default.

53 What is the status of the following C code when compiled by a standard-compliant C compiler?

c
#include <stdio.h>

void my_func(void); // Prototype specifies zero arguments

int main() {
my_func(42); // Called with one argument
return 0;
}

void my_func(void) {
printf("Hello!\n");
}

Function prototypes Hard
A. Results in a compilation error.
B. Compiles and runs, ignoring the extra argument.
C. Compiles with a warning, but runs correctly.
D. Results in undefined behavior at runtime.

54 What is the output of this C program demonstrating variable shadowing and lifetime?

c
#include <stdio.h>

int x = 5;

int get_x() {
static int x = 10;
return &x;
}

void set_x() {
int x = 20;
get_x() = x;
}

int main() {
printf("%d, ", x);
set_x();
printf("%d", x);
return 0;
}

Scope rules (local and global scope) Hard
A. 5, 10
B. 10, 20
C. 5, 20
D. 5, 5

55 What is the behavior of the following C code, which attempts to modify a const qualified variable via a pointer?

c
#include <stdio.h>

void attempt_modify(int p) {
p = 100;
}

int main() {
const int value = 50;
attempt_modify((int*)&value);
printf("%d", value);
return 0;
}

Function call including passing arguments by value and passing arguments by reference Hard
A. The behavior is undefined.
B. The program prints 50 as the modification fails silently.
C. Compilation error due to the cast (int*).
D. The program prints 100 as the const is overridden.

56 A function is defined using the old K&R style as shown. What does sizeof(arg) evaluate to inside the function when called with a char?

c
#include <stdio.h>

void K_and_R_func(arg)
char arg; // Parameter type declared here
{
printf("%zu", sizeof(arg));
}

int main()
{
K_and_R_func('A');
return 0;
}

Function definition Hard
A. This is not valid C and will not compile.
B. sizeof(double)
C. sizeof(int)
D. sizeof(char)

57 What will be printed by the following C program?

c
#include <stdio.h>
#include <math.h>

int main() {
double val1 = log(0.0);
double val2 = log10(-1.0);
double val3 = sqrt(-1.0);
int result = 0;
if (isinf(val1)) result += 1;
if (isnan(val2)) result += 2;
if (isnan(val3)) result += 4;
printf("%d", result);
return 0;
}

Math library functions Hard
A. 0
B. 3
C. 6
D. 7

58 What is the output of the following program that uses recursion to print a pattern?

c
#include <stdio.h>

void pattern(int n) {
if (n <= 0) {
return;
}
printf("%d ", n);
pattern(n - 2);
pattern(n - 3);
printf("%d ", n);
}

int main() {
pattern(5);
return 0;
}

Recursive functions Hard
A. 5 3 1 3 2 5
B. 5 3 2 5
C. 5 3 1 1 3 2 2 5
D. 5 3 1 1 3 5 2 2 5

59 Consider the following code snippet. Which statement is true?

c
void my_func() {
auto int a = 1; // Line 1
static int b = 1; // Line 2
a++;
b++;
printf("a=%d, b=%d\n", a, b);
}

int main() {
my_func();
my_func();
return 0;
}

Storage classes in C namely auto, Extern, Register, Static storage classes Hard
A. The output is a=2, b=2 followed by a=3, b=3.
B. The output is a=2, b=2 followed by a=2, b=3.
C. The output is a=2, b=2 followed by a=2, b=2.
D. The code has a syntax error because auto is not used this way.

60 Predict the output of the following C program involving pointer reassignment within a function.

c
#include <stdio.h>

struct Point { int x, y; };

void reassign(struct Point *p) {
struct Point new_p = {100, 200};
p->x = 50;
p = &new_p;
p->x = 300;
}

int main() {
struct Point my_p = {10, 20};
reassign(&my_p);
printf("%d %d", my_p.x, my_p.y);
return 0;
}

Function call including passing arguments by value and passing arguments by reference Hard
A. 50 200
B. 10 20
C. 300 200
D. 50 20