0% found this document useful (0 votes)
1 views

Code Based Test 3

The document outlines a sample test paper for C programming consisting of 15 questions to be completed in 40 minutes. Each question involves code snippets that need to be arranged or completed correctly. The test assesses knowledge of pointer arithmetic, memory allocation, control structures, and array initialization in C.

Uploaded by

tharunnstudies
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Code Based Test 3

The document outlines a sample test paper for C programming consisting of 15 questions to be completed in 40 minutes. Each question involves code snippets that need to be arranged or completed correctly. The test assesses knowledge of pointer arithmetic, memory allocation, control structures, and array initialization in C.

Uploaded by

tharunnstudies
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Sample Paper_C Proramming

Test Summary

No. of Sections: 1
Total Questions: 15
Total Duration: 40 mins

Section Summary

No. of Questions: 15
Duration: 40 mins

Q1) Arrange the code to use pointer arithmetic with characters:


Code Snippet
#include <stdio.h>
int main() {
1. return 0;
2. char str[] = "Pointer";
3. printf("%c", *(ptr+3));
4. str[3] = 'n';
5. ptr++;
6. char *ptr = str;
}

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

Q2) Arrange the code to copy array using pointer:


Code Snippet
#include <stdio.h>
int main() {
1. *(dest + i) = *(src + i);
2. int dest[3];
3. for (int i = 0; i < 3; i++)
4. return 0;
5. int src[] = {1, 2, 3};
}

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

Q4) Arrange the code to demonstrate multiple assignment. Shuffled Code:


Code Snippet
#include <stdio.h>
int main() {
1. x = y = z = 10;
2. int x, y, z;
3. printf("Values: %d %d %d", x, y, z);
4. return 0;
}

Options
1) 3 4 1 2
2) 2 1 3 4
3) 1 3 4 2
4) 4 3 1 2

Q5) Fill in the blanks to complete the code correctly. Output -1 6


Code Snippet
#include <stdio.h>

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>

void freeMemory(int *ptr) {


free(ptr); // Issue
}

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

Q15) Which of the following statements correctly initializes a 2D array in C?


A.
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
B.
int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
C.
int arr[3][4] = {{1, 2}, {3, 4}, {5, 6}};
D.
int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8};

Options
1) A. A, B, D
2) B. A, C, D
3) C. A, B, C
4) D. C, B, D

You might also like