KEMBAR78
Introduction To Programming in C | PDF | Theoretical Computer Science | Software
0% found this document useful (0 votes)
92 views12 pages

Introduction To Programming in C

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)
92 views12 pages

Introduction To Programming in C

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
You are on page 1/ 12

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,

You might also like