What is the output of the following code?
int a = 5;
if (a > 3) {
printf("Greater than 3");
} else {
printf("Less than or equal to 3");
}
Greater than 3
What does 'arr' represent if 'arr' is an array?
The address of the first element in the array.
What is the difference between `break` and `continue` in loop control statements?
`break` exits the loop, while `continue` skips to the next iteration.
What is the return type of a function that does not return a value?
In an `if-else` statement, what happens if the condition inside the `if` evaluates to false?
The code inside the `else` block is executed.
Consider the following function. What will be the result when called with `power(2, 3)`?
int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
8
What will the following code print?
int i = 0;
while (i < 3) {
printf("Hello ");
i++;
}
Hello Hello Hello
What does the keyword 'return' do in a function?
Terminates the function and optionally returns a value
What is the purpose of the `else` block in an `if-else` statement?
To execute code when the condition in the `if` statement is false.
Does the following function return the correct value of the largest number in an array?
int max(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < max) {
max = arr[i];
}
}
return max;
}
No, it returns the lowest value due to the condition.
In a `while` loop, what is the consequence of not modifying the loop variable within the loop body?
int count = 0;
while (count < 5) {
printf("Count: %d\n", count);
}
The loop will execute indefinitely.
What will be the output of the following code?
void multiply(int x, int y) {
printf("%d", x * y);
}
int main() {
multiply(2, 3);
return 0;
}
6
Consider the following code. What is the purpose of the condition inside the `if` statement?
int age = 20;
if (age >= 18) {
printf("You are an adult.");
} else {
printf("You are a minor.");
}
To check if the age is greater than or equal to 18.
Analyze the following function. Does it correctly calculate the average of the elements in the array?
float average(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum / n;
}
No, it performs integer division which may result in loss of precision.
What will be printed by the following code when `i = 0`?
int i;
for (i = 0; i < 3; i++) {
if (i == 1) {
continue;
}
printf("%d ", i);
}
0 2
What will be the output of the following code?
void printSquare(int x) {
printf("%d", x * x);
}
int main() {
printSquare(4);
return 0;
}
16
What will be the output of this code if the variable `temp` is set to 30?
int temp = 30;
if (temp < 0) {
printf("Freezing");
} else if (temp >= 0 && temp < 20) {
printf("Cold");
} else if (temp >= 20 && temp < 30) {
printf("Warm");
} else {
printf("Hot");
}
Hot
Evaluate the following function. What does it return when called with `sumArray(arr, 5)` where `arr = {1, 2, 3, 4, 5}`?
int sumArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
15
What will the following code print if `x` is initially 10?
int x = 10;
while (x > 0) {
printf("%d ", x);
x--;
}
10 9 8 7 6 5 4 3 2 1
Does the following code correctly swap the values of two variables?
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
No, it only swaps values locally but not outside the function.