Code Based Test 3
Code Based Test 3
Test Summary
No. of Sections: 1
Total Questions: 15
Total Duration: 40 mins
Section Summary
No. of Questions: 15
Duration: 40 mins
Options
1) 1 2 3 4 5 6 7
2) 6 7 4 5 2 1 3
3) 3 4 2 1 5 6 7
4) 2 4 6 5 3 1
Options
1) 4 3 2 1 5
2) 1 2 3 4 5
3) 4 1 2 3 5
4) 5 2 3 1 4
Q3) Arrange the code to implement a simple menu using goto. Shuffled Code:
Code Snippet
#include <stdio.h>
int main() {
1. printf("1. Continue\n2. Exit\nEnter choice: ");
2. menu:
3. if (choice == 2) goto end;
4. int choice;
5. end:
6. scanf("%d", &choice);
7. if (choice == 1) goto menu;
8. printf("Program exited\n");
return 0;
}
Options
1) 4 1 2 6 3 7 5 8
2) 4 2 1 6 3 7 5 8
3) 4 1 2 6 3 7 8 5
4) 4 1 2 6 7 3 5 8
Options
1) 3 4 1 2
2) 2 1 3 4
3) 1 3 4 2
4) 4 3 1 2
int main() {
_________________
a = a + b - (b *= a);
_________________
return 0;
}
Q6) Fill in the blanks to complete the code correctly. Output Odd Positive
Code Snippet
#include <stdio.h>
int main() {
int num = 7;
____________________
printf("Even\n");
} else {
printf("Odd\n");
}
___________________
printf("Positive\n");
} else {
printf("Negative\n");
}
return 0;
}
Q7) Fill in the blanks to complete the code correctly. Output Five
Code Snippet
#include <stdio.h>
int main() {
int num = 5;
________________
case 1:
printf("One\n");
break;
case 3:
_____________
break;
case 5:
_____________
break;
default:
printf("Invalid\n");
}
return 0;
}
Q8) Fill in the blanks to complete the code correctly. Output Non-divisible
Code Snippet
#include <stdio.h>
int main() {
________________
________________
printf("Non-divisible");
else
printf("Divisible");
return 0;
}
Q9) Identify the correct replacement for the marked issue so that the program prints "Number is
zero".
Code Snippet
#include <stdio.h>
int main() {
int num = 0;
if (num, 0) // Issue
printf("Number is zero");
return 0;
}
Options
1) if (num = 0);
2) if (num == 0);
3) if num = 0;
4) if (num != 0);
Q10) Identify the correct replacement for the marked issue so that the program correctly frees
memory.
Code Snippet
#include <stdio.h>
#include <stdlib.h>
int main() {
int x = 10;
freeMemory(&x);
return 0;
}
Options
1) Only free memory allocated with malloc/calloc/realloc
2) Use free(ptr) for any pointer, including stack memory
3) Always call free(ptr) twice to ensure complete deallocation
4) Assign ptr = NULL before calling free(ptr) to avoid errors
Q11) Identify the correct replacement for the marked issue so that the program prints "x is greater"
Code Snippet
#include <stdio.h>
int main() {
int x = 10, y = 5;
if x > y // Issue
printf("x is greater");
return 0;
}
Options
1) if (x > y)
2) if {x > y}
3) if x > y:
4) if (x, y > x)
Q12) Identify the correct replacement for the marked issue so that memory is allocated correctly
using malloc.
Code Snippet
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
arr = malloc(5); // Issue
return 0;
}
Options
1) arr =(int*)malloc(5 * sizeof(int));
2) arr = malloc(sizeof(arr));
3) arr = malloc(5 * int);
4) arr = (int*)malloc(arr * 5);:
Q13) Which of the following programs correctly use dynamic memory allocation with malloc()?
A.
#include
#include
int main() {
int *arr = (int*) malloc(5 * sizeof(int));
return 0;
}
B.
#include
#include
int main() {
int *arr;
arr = malloc(5 * sizeof(int));
return 0;
}
C.
#include
#include
int main() {
int *arr = malloc(5);
return 0;
}
D.
#include
#include
int main() {
int *arr = (int*) malloc(5);
return 0;
}
Options
1) A. A, B
2) B. A, C
3) C. A, D
4) D. A, B, C
Q14) Which of the following programs correctly prints all elements of a 1D array?
A.
#include
int main() {
int a[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++)
printf("%d ", a[i]);
return 0;
}
B.
#include
int main() {
int a[5] = {1, 2, 3, 4, 5};
for(int i = 1; i
printf("%d ", a[i]);
return 0;
}
C.
#include
int main() {
int a[] = {10, 20, 30};
printf("%d", a[3]);
return 0;
}
D.
#include
int main() {
int a[3];
a[0] = 5; a[1] = 6; a[2] = 7;
for(int i = 0; i < 3; i++)
printf("%d ", a[i]);
return 0;
}
Options
1) A. A, D
2) B. A, B
3) C. B, D
4) D. C, D
Options
1) A. A, B, D
2) B. A, C, D
3) C. A, B, C
4) D. C, B, D