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 define the actual logic of the function.
B. To call the function from the main() function.
C. To declare the function's name, return type, and parameters to the compiler before it is defined.
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 header and the function body
B. The function prototype and the function arguments
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. The memory address of the argument
B. A copy of the argument's value
C. A pointer to the argument
D. The original argument itself

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

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

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

Scope rules (local and global scope) Easy
A. external variable
B. local variable
C. global 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. extern
B. auto
C. register
D. static

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

Math library functions Easy
A. <math.h>
B. <stdlib.h>
C. <stdio.h>
D. <string.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 the & symbol in the function definition
D. By using global variables

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 the main() function
C. From any function in the program
D. Only from functions within the same file

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

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

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 defined in another source file or later in the same file.
C. The variable should be stored outside of the computer's memory.
D. The variable is external to all functions.

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

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

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

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

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

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

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 address
C. Pass by reference
D. Pass by pointer

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. a CPU register
B. static memory
C. main memory (RAM)
D. a file on the disk

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 local variable takes precedence inside that function.
B. The global variable takes precedence inside that function.
C. The program will crash at runtime.
D. The program will result in a compilation error.

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

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

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 creates a reference to the variable num.
B. It doubles the value of the variable num.
C. It provides the value of the variable num.
D. It provides the memory address 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 linker error will occur because the function object code cannot be found.
B. A runtime error will occur immediately upon calling the function.
C. The program will not compile, citing a syntax error.
D. The program will compile with a warning, and the compiler will assume a default prototype int function_name(), which may lead to runtime errors.

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 = 10, b = 20
B. a = 20, b = 20
C. Compiler Error
D. a = 20, b = 10

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 20 5
C. 5 10 10
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. 1 6 11
B. 1 1 1
C. 1 5 10
D. Compiler Error

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. 6
D. 10

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. 9.0
B. 8.2
C. 7.0
D. 8.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] = 1, n = 10
B. numbers[0] = 100, n = 10
C. numbers[0] = 1, n = 5
D. numbers[0] = 100, n = 5

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

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

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. 25
B. 10011
C. The function runs indefinitely.
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. 10 10
B. 20 20
C. 10 20
D. Compiler Error

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 fails to compile due to a type mismatch in the function call.
B. The code compiles and prints "n = 6".
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 cannot be modified after initialization.
B. A static global variable has internal linkage, limiting its visibility to the translation unit (the file) in which it is defined.
C. A static global variable retains its value across program executions, while a regular global does not.
D. A static global variable is stored on the stack, while a regular global is stored in the data segment.

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. 20
B. 10
C. Compiler Error
D. Undefined behavior (printing a value from a dangling pointer).

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 = abs(pow(x, 3.0));
C. y = pow(abs(x), 3.0);
D. y = fabs(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. auto storage class, and its initial value is indeterminate (garbage).
B. auto storage class, and its initial value is 0.
C. static storage class, and its initial value is 0.
D. register 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. extern float calculate(float a, int b);
C. float calculate(float, int);
D. float calculate(float value, int count);

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 will always result in a stack overflow for n > 2.
C. The base case is incorrect, leading to an infinite recursion.
D. It cannot calculate Fibonacci numbers for even values of n.

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. 2 4
B. 4 4
C. 2 2
D. 0 2

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. 14
B. 12
C. 5
D. 16

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=10, b=20, pa=20, pb=10
C. a=100, b=200, pa=100, pb=200
D. a=200, b=10, pa=10, pb=200

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. 1, 1
C. 10, 10
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 2
B. 6 5 4 3 2 1
C. 6 5 4 2 1
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. A linker error occurs.
B. A compilation error occurs in f1.c.
C. The program compiles and prints 100.
D. The program compiles and prints 0.

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(int), which is typically 4.
B. It prints sizeof(long double), which is typically 10, 12, or 16.
C. It results in a runtime crash.
D. It results in a compilation error.

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. 1.0 1.0 1.0
B. 1.0 1.0 -2.0
C. nan 1.0 -2.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 pointer cannot point to a register variable.
B. A register variable cannot be initialized.
C. The address-of operator (&) cannot be applied to a register variable.
D. A register variable is read-only after initialization.

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. Stack Overflow
B. 11
C. 7
D. 9

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. 8,80
B. 80,8
C. 80,80
D. 8,8

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. 20
B. Undefined Behavior
C. 15
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 tentative definition that will be initialized to 0 by default.
B. It is a full definition, not just a declaration. It allocates storage for var and provides an initial value.
C. It is a syntax error because extern declarations cannot have initializers.
D. It is a declaration, and the program requires another file to provide the definition of var.

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. Compiles with a warning, but runs correctly.
B. Results in undefined behavior at runtime.
C. Results in a compilation error.
D. Compiles and runs, ignoring the extra argument.

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, 20
B. 5, 5
C. 5, 10
D. 10, 20

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(char)
C. sizeof(double)
D. sizeof(int)

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

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 5 2 2 5
D. 5 3 1 1 3 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=2, b=2.
B. The output is a=2, b=2 followed by a=3, b=3.
C. The output is a=2, b=2 followed by a=2, b=3.
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