Pps QB
Pps QB
Copy code
int x = 5;
if (x > 3) {
printf("Greater\n");
} else {
printf("Smaller\n");
}
a) Greater
b) Smaller
c) No output
d) Error
Answer: a) Greater
Answer: b) if (x > 0)
int a = 10;
if (a == 10)
printf("Ten\n");
else
printf("Not Ten\n");
a) Ten
b) Not Ten
c) Error
d) No output
Answer: a) Ten
int b = 5;
if (b < 5)
printf("Less\n");
else if (b == 5)
printf("Equal\n");
else
printf("More\n");
a) Less
b) Equal
c) More
d) No output
Answer: b) Equal
a) Positive
b) Non-positive
c) Error
d) No output
Answer: b) Non-positive
a) Five
b) Not Five
c) Error
d) Infinite loop
a) A
b) B
c) C
d) D
Answer: c) C
int x = 20;
if (x < 10)
printf("Less than 10\n");
else if (x < 20)
printf("Less than 20\n");
else
printf("20 or more\n");
a) Less than 10
b) Less than 20
c) 20 or more
d) No output
Answer: c) 20 or more
if (a > 0) {
if (a < 10) {
printf("Single digit\n");
}
}
a) True
b) False
c) Error
d) It won't compile
Answer: a) True
int y = 15;
if (y < 10) {
printf("Small\n");
} else if (y < 20) {
printf("Medium\n");
} else {
printf("Large\n");
}
a) Small
b) Medium
c) Large
d) No output
Answer: b) Medium
if (x > 0) {
if (x == 1)
printf("One\n");
else
printf("Not one\n");
}
a) Correct
b) Incorrect
c) Error
d) Will produce no output
Answer: a) Correct
5. What will be the output of this code?
int a = 5;
if (a > 0) {
if (a < 3)
printf("Less than 3\n");
else
printf("Greater than or equal to 3\n");
} else {
printf("Negative\n");
}
a) Less than 3
b) Greater than or equal to 3
c) Negative
d) No output
int num = 8;
if (num % 2 == 0) {
if (num > 10)
printf("Greater than 10\n");
else
printf("Even and less than or equal to 10\n");
} else {
printf("Odd\n");
}
a) Greater than 10
b) Even and less than or equal to 10
c) Odd
d) No output
a) A
b) B
c) C
d) D
Answer: b) B
int z = -5;
if (z < 0) {
if (z < -10)
printf("Less than -10\n");
else
printf("Between -10 and 0\n");
} else {
printf("Non-negative\n");
}
int x = 2;
switch (x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Default\n");
}
a) One
b) Two
c) Default
d) No output
Answer: b) Two
int num = 3;
switch (num) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
// No break here
case 3:
printf("Three\n");
break;
default:
printf("Default\n");
}
a) One
b) Two
c) Three
d) Two\nThree
Answer: d) Two\nThree
5. What happens if you omit the break statement in a switch case? a) The program will
crash.
b) It will skip to the next case and continue executing.
c) It will terminate the program.
d) Nothing; it behaves like an if statement.
a) Valid
b) Invalid due to no break in default
c) Invalid due to the data type of value
d) Invalid syntax
Answer: a) Valid
a) Excellent
b) Well done
c) You passed
d) Invalid grade
int i = 1;
switch (i) {
case 1:
printf("Case 1\n");
case 2:
printf("Case 2\n");
break;
case 3:
printf("Case 3\n");
break;
default:
printf("Default case\n");
}
a) Case 1
b) Case 2
c) Case 1 Case 2
d) Case 1 Case 2 Default case
int day = 5;
switch (day) {
case 1:
printf("Monday\n");
break;
case 5:
printf("Friday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Other day\n");
}
a) Monday
b) Friday
c) Sunday
d) Other day
Answer: b) Friday
MCQ ON UNCONDITIONAL CONTROL STATEMENTS: BREAK,
CONTINUE, AND GOTO
1. What does the break statement do in a loop? a) It pauses the execution of the loop.
b) It skips the current iteration and continues with the next.
c) It terminates the loop and transfers control to the statement following the loop.
d) It causes an infinite loop.
Answer: c) It terminates the loop and transfers control to the statement following the
loop.
a) 0 1 2 3 4
b) 0 1
c) 0 1 2
d) No output
Answer: b) 0 1
3. What is the purpose of the continue statement? a) To terminate the loop immediately.
b) To skip the current iteration and move to the next iteration of the loop.
c) To exit the entire program.
d) To reset the loop counter.
Answer: b) To skip the current iteration and move to the next iteration of the loop.
a) 0 1 2 3 4
b) 0 1 3 4
c) 0 1 2 3
d) No output
Answer: b) 0 1 3 4
5. Which of the following statements is true regarding the goto statement? a) It can
only jump to the end of a function.
b) It can only jump to a label defined within the same block.
c) It makes code easier to read.
d) It can transfer control to any label in the program.
int i = 0;
loop:
printf("%d ", i);
i++;
if (i < 3) goto loop;
a) 0 1 2 3
b) 0 1 2
c) 1 2 3
d) No output
Answer: b) 0 1 2
7. What happens if you use goto to jump into a block that contains variable
declarations? a) The program will crash.
b) It will work as expected.
c) The variables will not be initialized.
d) It leads to a compilation error.
Answer: c) goto
a) 0 1 2 3 4
b) 0 1 2
c) 0 2
d) 0 1 2 3
Answer: c) 0 2
a) 1 1
b) 1 1
12
21
22
c) 1 1
21
d) No output
Answer: b)
Copy code
1 1
1 2
2 1
2 2
int i = 0;
while (i < 2) {
int j = 0;
while (j < 3) {
printf("%d %d\n", i, j);
j++;
}
i++;
}
a) 0 0
10
b) 0 0
01
02
10
11
12
c) 0 0
01
02
10
d) No output
Answer: b)
Copy code
0 0
0 1
0 2
1 0
1 1
1 2
a) 1 1
12
21
22
b) 1 1
21
c) 1 1
12
13
21
d) No output
Answer: b)
Copy code
1 1
2 1
4. How many times will the inner loop execute in the following code?
a) 2
b) 3
c) 5
d) 6
Answer: d) 6
a) 0 0 0
001
010
011
100
101
110
111
b) 0 0 0
001
111
c) 0 0 0
011
100
d) No output
Answer: a)
Copy code
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
int count = 0;
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
count++;
}
}
printf("%d\n", count);
a) 2
b) 3
c) 5
d) 6
Answer: d) 6
int i = 1;
while (i <= 2) {
int j = 1;
while (j <= 2) {
printf("%d %d\n", i, j);
j++;
}
i++;
}
a) 1 1
12
21
22
b) 1 1
22
c) 1 2
21
d) No output
Answer: a)
Copy code
1 1
1 2
2 1
2 2
int i = 0;
while (i < 5) {
for (int j = 0; j < 3; j++) {
// Some code
}
i++;
}
a) 3
b) 5
c) 8
d) 15
Answer: d) 15
a) 1
12
b) 1 2
12
c) 1
12
123
d) No output
Answer: a)
MCQ ON ARRAYS, ONE-DIMENSIONAL ARRAYS, AND TWO-
DIMENSIONAL ARRAYS
a) 1
b) 2
c) 3
d) 0
Answer: b) 2
3. Which of the following statements is true regarding array indices in C? a) They start
from 1.
b) They start from 0.
c) They can be negative.
d) They must be declared explicitly.
4. How do you access the first element of an array named arr? a) arr[0]
b) arr[1]
c) arr[2]
d) first(arr)
Answer: a) arr[0]
a) 4
b) 5
c) 6
d) 7
Answer: c) 6
int arr[4][5];
a) 4
b) 5
c) 20
d) 9
Answer: c) 20
a) 10
b) 20
c) 30
d) 40
Answer: d) 40
10. What will happen if you try to access an element outside the bounds of an array? a)
The program will compile successfully.
b) It will give a runtime error.
c) It may return garbage value or cause undefined behavior.
d) It will reset the value to zero.
Answer: c) 13
Answer: b) &
int x = 5;
int *p = &x;
printf("%d", *p);
a) 5
b) &x
c) p
d) Error
Answer: a) 5
a) 10
b) 20
c) &var
d) Error
Answer: b) 20
int a = 10;
int *ptr = &a;
printf("%p", ptr);
int a = 5;
int *ptr = &a;
printf("%d", *ptr + 10);
a) 5
b) 10
c) 15
d) 20
Answer: c) 15
12. If int *p; is declared, what does p point to initially? a) 0
b) Random memory location
c) Undefined value
d) The address of the first integer
int a = 2;
int *p = &a;
a += 3;
printf("%d", *p);
a) 2
b) 3
c) 5
d) Error
Answer: c) 5
Answer: c) To access the value stored at the address a pointer is pointing to.
int a = 10;
int *p = &a;
printf("%d", *p);
a) 10
b) &a
c) p
d) 0
Answer: a) 10
6. Given the declaration int *p;, which of the following statements correctly
initializes p to point to an integer variable x?
7. a) p = x;
b) p = &x;
c) *p = x;
d) p = *x;
Answer: b) p = &x;
8. What happens if you try to dereference a pointer that has not been initialized?
9. a) It will print 0.
b) It will cause a runtime error.
c) It may cause undefined behavior or a segmentation fault.
d) It will return the address of the variable.
int a = 5;
int *p = &a;
*p = 20;
printf("%d", a);
a) 5
b) 20
c) 15
d) 0
Answer: b) 20
Answer: b) *ptr;
13. What will happen if you dereference a pointer that points to an invalid memory
location? a) It will print a garbage value.
b) It will cause a compilation error.
c) It will cause a segmentation fault or crash the program.
d) It will return 0.
int x = 7;
int *p = &x;
x = *p + 1;
printf("%d", x);
a) 7
b) 8
c) 6
d) Error
Answer: b) 8
10. Which of the following correctly increments the value pointed to by a pointer ptr?
a) ptr++;
b) *ptr++;
c) (*ptr)++;
d) ptr = ptr + 1;
Answer: c) (*ptr)++;
1. What is a void pointer in C? a) A pointer that does not point to any variable.
b) A pointer that can point to any data type.
c) A pointer that only points to integers.
d) A pointer that cannot be dereferenced.
Answer: b) 0
a) 0
b) NULL
c) Some random address
d) Error
Answer: a) 0
13. Which of the following statements about void pointers is false? a) They can be
assigned the address of any data type.
b) They can be dereferenced directly without casting.
c) They are often used for generic data structures.
d) They require casting before dereferencing.
14. What will happen if you attempt to dereference a null pointer? a) It will print 0.
b) It may cause a segmentation fault or crash the program.
c) It will return a null value.
d) It will compile successfully but yield garbage value.
10. How do you convert a void pointer to a specific pointer type? a) Direct assignment
b) Casting
c) Using the sizeof operator
d) You cannot convert it
Answer: b) Casting
Answer: b) 20
4. Which of the following correctly accesses the third element of an array using a
pointer?
5. a) *ptr[2];
b) ptr[2];
c) *(ptr + 2);
d) Both b and c
a) 1
b) 2
c) 3
d) 5
Answer: b) 2
a) 0
b) 5
c) 10
d) 15
Answer: b) 5
a) 1 2
b) 1 3
c) 2 3
d) 2 1
Answer: b) 1 3
10. What will be the value of arr[1] after executing the following code?
a) 1
b) 2
c) 6
d) 5
Answer: c) 6
11. Which of the following statements is true regarding pointer arithmetic? a) Adding 1
to a pointer moves it to the next byte.
b) Adding 1 to a pointer moves it to the next element of the type it points to.
c) Subtracting a pointer from another gives the size of the type it points to.
d) Both b and c are true.
Answer: c) 30
PART B
1. Write a program to check if a given number is positive, negative, or zero using a simple if
statement.
2. Develop a program that takes three integer inputs and prints the largest number using the
if...else statement.
3. Write a program to classify a student's grade based on the following marks:
=90: A
80-89: B
70-79: C
<70: Fail
Use else if and nested if statements.
4. Create a calculator program using switch case that performs addition, subtraction,
multiplication, or division based on user input.
5. Write a program to print the first 10 Fibonacci numbers using a for loop.
6. Create a program to reverse a given integer number using a while loop.
7. Write a program that repeatedly prompts the user for a number and calculates the sum of
all numbers entered. The loop should stop when the user enters zero. Use a do...while
loop.
8. Write a program to declare, initialize, and access a 1D array of integers. Find and print
the sum of all elements in the array.
9. Write a program to declare, initialize, and display a 2D array. Find and print the sum of
all elements in each row.
10. Write a program that uses pointers to reverse the elements of a 1D array.
PART C
1. Develop a program for a school where students' grades are calculated based on marks
entered by the user. The system should accept marks for five subjects and then compute
the average and the final grade using conditional control statements (if...else, nested if).
The grade should be displayed as:
2. Create a program that simulates an ATM withdrawal process. The system should prompt
the user for the account type (Savings, Current), ask for the amount to withdraw, and
ensure that the balance is sufficient before proceeding with the transaction. Use switch
case for account type selection and conditional statements for balance checking. Include
the option to cancel the transaction and return to the main menu.
3. Write a program for an online shopping cart system where a user can add up to 5 items
(represented as prices) to their cart using a 1D array. The system should allow users to
view the total amount, apply a discount based on the total (10% off if the total is above
$100), and display the final amount after applying the discount. Use loops and
conditional statements to handle cart operations and discounts.
4. Design a program that accepts two 2D matrices of size 3x3 from the user. The program
should perform matrix addition and display the resulting matrix. Additionally, provide
functionality for checking whether the matrices are equal or not. Use nested for loops and
2D arrays to implement matrix addition and comparison.
5. Write a program that dynamically allocates memory for a 1D array of integers using
pointers. The program should allow the user to input the size of the array, enter the
values, and then display the array. Implement a function to reverse the array in place
using pointer manipulation and display the reversed array.