NEW
Font size
WorksheetsCPROG 1 | LONG QUIZ - FINALS
Total questions: 60
Worksheet time: 3hrs 0mins
Which of the following is the primary purpose of selection constructs in C?
To repeat a block of code multiple times
To allow the program to make choices based on conditions
To store multiple values in a single variable
To define a function
In an if-else statement, when is the code inside the else block executed?
When the condition in the if statement is true
When the condition in the if statement is false
Always, regardless of the condition
Only when the program encounters an error
What is the correct syntax for a switch statement?
switch (condition) { case value: ... }
switch { expression } { case value: ... }
switch (expression) { case value: ... }
select (expression) { option value: ... }
What is the function of the break statement inside a switch case?
It terminates the program.
It restarts the switch statement.
It exits the switch structure to prevent 'fall-through' to the next case.
It skips the current iteration of a loop.
Which data types are allowed in the expression of a switch statement in C?
float and double
int and char
Strings only
All data types
What happens if no case matches the expression in a switch statement?
The program crashes.
The default block is executed, if present.
The first case is executed.
The compiler throws an error.
Which operator is used to check for equality in an if condition?
=
==
!=
===
What is a "nested" if statement?
An if statement that acts as a loop.
An if statement located inside another if or else block.
An if statement with multiple conditions separated by commas.
An if statement that checks for errors.
In C, any non-zero value in a condition is treated as:
False
Null
True
Undefined
Which construct is best suited for selecting one option from a list of fixed integer values?
while loop
if-else ladder
switch statement
for loop
What are the three parts of a standard for loop definition?
Start, Stop, Pause
Initialization, Condition, Increment/Decrement
Declaration, Execution, Termination
Input, Process, Output
Which loop is known as an "entry-controlled" loop?
do-while loop
while loop
goto loop
None of the above
What is the key difference between a while loop and a do-while loop?
A while loop runs at least once; a do-while might not run at all.
A do-while loop executes its body at least once, even if the condition is false.
do-while loops cannot use counters.
while loops are used only for infinite loops.
Which statement is used to skip the rest of the current loop iteration and return to the condition check?
break
stop
continue
return
How do you create an infinite loop using the for construct?
for(1, 10, 1)
for(;;)
for(infinite)
loop(forever)
The goto statement is generally discouraged because:
It is slower than loops.
It makes the program flow difficult to trace and debug.
It takes up more memory.
It is not supported in modern C compilers.
What happens if the condition in a while loop is initially false?
The loop executes once.
The loop body is never executed.
The program reports a syntax error.
The loop becomes infinite.
In the statement for (i = 0; i < 5; i++), when is i++ executed?
Before the condition check.
Before the loop body executes.
After the loop body executes.
At the very start of the loop.
Which flowchart symbol typically represents the condition in a loop?
Rectangle
Diamond
Oval
Parallelogram
What is the output of `int i = 0; while(i < 3) { printf("%d ", i); i++; }`?
A. 0 1 2 3
B. 1 2 3
C. 0 1 2
D. 0 1
How is a two-dimensional array best visualized?
A. As a single line of numbers
B. As a stack of cards
C. As a matrix or table with rows and columns
D. As a tree structure
Which syntax correctly declares a 2D array with 3 rows and 4 columns?
A. `int arr[3, 4];`
B. `int arr[3][4];`
C. `int arr[4][3];`
D. `array arr[3][4];`
How does C store two-dimensional arrays in memory?
A. Column-major order
B. Row-major order
C. Random order
D. Linked list format
How many integers can the array `int matrix[3][5]` hold?
A. 8
B. 15
C. 35
D. 3
To access the element in the second row and third column of `arr`, you would use:
A. `arr[2][3]`
B. `arr[1][2]`
C. `arr[3][2]`
D. `arr[2][1]`
If you initialize `int mat[2][2] = {1, 2, 3, 4};`, what is the value of `mat[1][0]`?
A. 1
B. 2
C. 3
D. 4
What is commonly used to traverse all elements of a 2D array?
A single while loop.
Nested for loops.
A switch statement.
A pointer to a function.
In the declaration `int arr[2][3] = {{1, 2}, {4, 5, 6}};`, what happens to the uninitialized element in the first row?
It is set to 0.
It contains a garbage value.
It causes a syntax error.
It takes the value of the next element (4).
Which loop variable typically represents the "row" in a standard nested loop traversal?
The inner loop variable.
The outer loop variable.
Both variables simultaneously.
The variable declared in `main`.
Valid indices for `int grid[3][3]` range from:
1 to 3 for both dimensions.
0 to 3 for both dimensions.
0 to 2 for both dimensions.
1 to 4 for both dimensions.
What is a function in C?
A variable that changes value.
A block of code that performs a specific task.
A type of loop.
A header file.
What keyword is used if a function does not return a value?
`null`
`int`
`empty`
`void`
The values passed to a function when it is called are known as:
Parameters
Arguments
Prototypes
Returns
Where does the execution of a C program always begin?
The first included library.
The start() function.
The main() function.
The first defined user function.
What is the scope of a variable declared inside a function?
Global (accessible everywhere).
Local (accessible only within that function).
File (accessible anywhere in the file).
Universal.
Which of the following is a valid function prototype?
int add(int a, int b)
function add(a, b);
int add(int, int);
void add;
What does the return statement do?
Repeats the function.
Prints a value to the screen.
Ends the function and optionally sends a value back to the caller.
Declares a new function.
Which principle does using functions support?
Redundancy
Complexity
Reusability
Hard-coding
If you pass an array to a function void func(int arr[]), what is actually passed?
A copy of the entire array.
The value of the first element only.
The address of the first element (pointer).
The size of the array.
To call a function named `calculate` with the value 5, you write:
call calculate(5);
calculate[5];
calculate(5);
void calculate(5);
What is recursion?
A loop that never ends.
A process where a function calls itself.
A function that calls `main()`.
A variable that stores its own address.
What is a "base case" in recursion?
The initial call to the function.
The most complex part of the problem.
The condition that stops the recursion.
The first variable declared.
What happens if a recursive function lacks a base case?
It runs faster.
It returns 0.
It causes a stack overflow (infinite recursion).
It automatically fixes itself.
In the factorial example `n * factorial(n - 1)`, what does this expression represent?
The base case.
The recursive case.
The termination condition.
The loop counter.
Which data structure is used internally to manage recursive calls?
Queue
Linked List
Stack
Array
Indirect recursion occurs when:
Function A calls Function A.
Function A calls Function B, and Function B calls Function A.
A loop is used inside a function.
A function calls itself without parameters.
Calculating the Fibonacci sequence is a classic example of:
Sorting
Recursion
String manipulation
File I/O
Why might an iterative solution be preferred over a recursive one?
Recursion is always harder to write.
Iteration uses less memory (no stack overhead).
Recursion cannot return values.
Iteration is the only way to solve math problems.
What is "tail recursion"?
When the recursive call is the very last action in the function.
When the function returns void.
When the base case is at the end of the function.
When the function calls main.
In the call fibonacci(0), what should be returned based on standard definition?
0
1
-1
null
What does a pointer variable store?
An integer value.
A character.
The memory address of another variable.
The value of the variable it points to.
Which operator is used to get the address of a variable?
*
&
%
->
What does the * operator do when used with a pointer variable (e.g., *ptr)?
It returns the address stored in the pointer.
It declares a new pointer.
It dereferences the pointer (accesses the value at the address).
It multiplies the pointer by a value.
How do you declare a pointer to an integer?
int &ptr;
int *ptr;
pointer int ptr;
int ptr*;
If int x = 10; and int *p = &x;, what is the value of *p?
The address of x.
10
Random garbage value.
0
What is a Null Pointer?
A pointer that points to address 0 or nowhere.
A pointer that points to a negative number.
A pointer that has not been initialized.
A pointer to a void function.
If ptr is an integer pointer pointing to address 1000, and sizeof(int) is 4, what is ptr + 1?
1001
1004
1002
2000
How are arrays related to pointers in C?
Arrays are pointers that cannot be changed.
The array name acts as a constant pointer to the first element.
Pointers cannot be used with arrays.
They are completely unrelated.
What allows a function to modify a variable defined in the calling function?
Passing by value.
Passing by reference (using pointers).
Using global variables only.
Recursion.
What is the correct format specifier to print a memory address in C?
%d
%f
%p
%a
