0% found this document useful (0 votes)
59 views12 pages

C Programming Exam Questions & Answers

This document contains a sample exam for an introduction to C programming course. It includes 12 multiple choice questions across 3 sections testing knowledge of C fundamentals like loops, functions, pointers, and arrays. The questions provide code snippets and ask for the output or to complete the code to solve a problem.

Uploaded by

Priya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views12 pages

C Programming Exam Questions & Answers

This document contains a sample exam for an introduction to C programming course. It includes 12 multiple choice questions across 3 sections testing knowledge of C fundamentals like loops, functions, pointers, and arrays. The questions provide code snippets and ask for the output or to complete the code to solve a problem.

Uploaded by

Priya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Programming in C

Sample Questions (Duration of the paper and the number of questions per section
may be different)

Section1: Each question carries one mark.

1. The output of the following code is ____________

#include <stdio.h>
int main()
{
int c=1;

switch( c ){
case 1:
printf(“1”);
default:
printf(“0”);
}

return 0;
}

Answer: ​10
2. The output of the following code is _____________________

#include <stdio.h>
int main()
{
int i=0;

for(i=0; i<10; i++){


if(i==2){
continue;
}
printf(“%d,”, i);
}

return 0;
}

Answer: ​0,1,3,4,5,6,7,8,9,
3. The output of the following code is ____________________________

#include <stdio.h>

int main()
{
int i=2;

do{
i--;
printf(“%d,”, i);
}while(i>0);

return 0;
}

Answer: ​1,0,
4. In C, static variables are initialized only once during the execution of a program, and the
values are not destroyed until the program terminates. With this in mind, the output of the
following code is ___________

#include <stdio.h>
int foo()
{
static int a=0;
a++;
return a;
}
int main()
{
printf(“%d,”, foo());
printf(“%d,”, foo());
return 0;
}

Answer: ​1,2,
Section 2: Each question carries two marks

5. The output of the following code on input ​1 3 -1​ is _______________________.

#include <stdio.h>

int main()
{
int i, count = 0;
scanf(“%d”, &i);

while (i != -1){
count ++;
scanf(“%d”,&i);
}
printf(“%d”, count);
return 0;
}

Answer: ​2
6. The output of the following code on input ​-1 -1​ is _________________________.

#include <stdio.h>

int main()
{
int i, count = 0;
scanf(“%d”, &i);
do{
count ++;
scanf(“%d”,&i);
}while( i != -1);
printf(“%d”, count);
return 0;
}

Answer: ​1
7. The output of the following code is _________________________.

#include <stdio.h>
void foo(int);
void bar(int);

void foo(int a)
{
if(a==0){
return;
}
printf("%d,", a);
bar(a-1);
return;
}

void bar(int a)
{
if(a==0){
return;
}
printf("%d,", a);
foo(a/2);
return;
}

int main()
{
foo(10);
return 0;
}
Answer: ​10,9,4,3,1, 
 
8. The following code allocates a variable sized array, reads the array from the input and prints
the array. Fill the expression in the blank which forms valid C code below:
________________________

#include <stdio.h>
#include <stdlib.h>
int count;
void print_array(int a[])
{
int i;
for(i=0;i<_____; i++){
printf(“%d ”, a[i]);
}
return;
}
int main()
{
int *a;
int n;
int i=0;
scanf(“%d”,&n);
count=n;
a = (int *)malloc(n*sizeof(int));
for(i=0;i<n;i++){
scanf(“%d”,&a[i]);
}
print_array(a);
return 0;
}

Answer: ​count
Section 3: All questions carry 3 marks each

9. The output of the following code is____________________

#include <stdio.h>
int main()
{
int i,j=0,k;
printf("%d,",3>5-2);
k = i=2,j;
printf("%d",k);
}

Answer:​ 0,2,
10. The following function returns the length of a string. Which of the following statements will
correctly implement the function?

ptrdiff_t _strlen(char *s)


{
char* i=s;
while(*s){
________________
}
return s-i;
}

A) *s++;
B) s++;
C) *s = s+1;
D) s = *s+1;

Answer: ​B
11. A​ n inversion in an array a[] are two elements a[i] and a[j] such that i<j but a[i]>a[j]. The
following code prints all the inversions in the array in the ordered pair form (a[i], a[j]) where
i<j, and counts the number of inversions in an array. Which of the following completes the
code?

int inversion_count(int *a, int n)


{
int i;
int j;
int count;

for(_______;i<n;i++){
for(______;j<n;j++){
if(a[i]>a[j]){
printf("(%d,%d)\n",a[i],a[j]);
count++;
}
}
}
return count;
}

A) i=0 ​in the outer loop​, ​and​ j=0 in ​the inner loop
B) i=0 ​in the outer loop​ and j=1 ​in the inner loop
C) i=0, count=0 ​in the outer loop and ​j=i+1 ​in the inner loop
D) i=0 ​in the outer loop and ​j=i, count=0 ​in the inner loop

Answer: ​C
12. The following code tries to swap the contents of the integers pointed to by ptra and ptrb.
The output of the following code is ________________

#include <stdio.h>
void swap(int *ptra, int *ptrb)
{
int *ptrtemp;
ptrtemp = ptra;
ptra = ptrb;
ptrb = ptrtemp;
return;
}

int main()
{
int a=1,b=2;
swap(&a,&b);
printf("%d,%d,", a,b);
return 0;
}

Answer:​ 1,2,

Common questions

Powered by AI

The 'continue' statement in C causes the current iteration of the loop to end immediately, and control jumps to the next iteration of the loop. In the provided code example from Source 1, when 'i' equals 2, the 'continue' statement skips the rest of the loop body and continues with the next cycle, resulting in the output "0,1,3,4,5,6,7,8,9," .

A 'do-while' loop guarantees execution of its block at least once because the condition is checked after the loop block. In contrast, a 'while' loop might not execute if the condition is false initially. For instance, when given an input of '-1 -1', the 'do-while' loop in Source 1 executes once before checking that 'i' equals '-1', counting 1 iteration, unlike a 'while' loop which would not execute at all under similar conditions .

To determine the length of a string using pointer arithmetic in C, iterate the pointer until it reaches the null character '\0' while incrementing the pointer. The difference between the final pointer position and the initial pointer gives the string length. The correct statement completing the code in Source 1 for this logic is 's++;' as it moves the pointer to the next character in the string .

An inversion in an array occurs when there are two elements such that the first element is greater than the second, and their indices follow an increasing order. Efficiently counting inversions involves using nested loops to iterate through the array: starting the outer loop from the first element and the inner from the next element to the current outer loop element. This structure allows a direct comparison and counting of inversions, as seen in Source 2's correct answer C, using initializations 'i=0, count=0' for the outer and 'j=i+1' for the inner loop .

In C, dynamic memory allocation for variable-sized arrays is performed using 'malloc' or 'calloc'. After allocating memory based on the required size, as shown in Source 1 with 'malloc(n*sizeof(int))', inputs are read into the array, and operations like 'print_array' can be performed using a size-based loop, as indicated by using 'count' as the loop boundary . This ensures proper memory access, and the allocated memory should be freed using 'free(a)' after its usage to prevent memory leaks.

When using the comma operator in C, the expression evaluates from left to right, but only the result of the rightmost expression is assigned. In the example from Source 1, 'k = i=2,j;' assigns the value '2' to both 'i' and 'k' because 'i=2' is evaluated before 'j', and 'j' is not assigned to either 'i' or 'k' .

Static variables in C retain their value between function calls because they are initialized only once and their value persists throughout the program's lifetime. For example, the code in Source 1 uses a static variable 'a' within the 'foo' function, which increments each time the function is called. Hence, it outputs '1' on the first call and '2' on the second call .

Mutually recursive functions are those that call each other. In C, such functions leverage call stacks to interleave execution. For instance, in the code from Source 1 with 'foo' and 'bar', 'foo(10)' calls 'bar(9)', which further calls 'foo(4)' and so on. This leads to an interleaved execution flow resulting in the output sequence '10,9,4,3,1,' as each function completes its logic and returns, deepening and unwinding the call stack alternately .

In C, when a 'switch' statement case does not have a 'break' keyword, the execution continues into the next case, regardless of whether the case matches. This is known as 'fall-through'. For example, the code provided in Source 1 prints "10" because, after printing "1" for 'case 1', it falls through to the 'default' case, which prints "0" .

The provided code attempts to swap integers using pointers but fails because it only swaps the local copies of the pointers, not the integers themselves. Swapping the local pointers 'ptra' and 'ptrb' doesn't affect 'a' and 'b', hence they print as '1,2', unchanged. A successful swap should directly modify the address contents using temporary storage for values, not pointers .

You might also like