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

C Programming MCQ

Technical questions

Uploaded by

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

C Programming MCQ

Technical questions

Uploaded by

shelukutty.03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Programming MCQ

1) What will be the output of the program?


int main()
{
printf("%d ",printf(" Cprog "));
return 0;
}

1. Cprog
2. Cprog 5
3. Cprog 7
4. Cprog7
Ans: option 3
Explanation: First, " Cprog " is printed by the inner printf. Then, 7 (the number of characters printed by
the inner printf) is printed by the outer printf, followed by a space.

2) What will be the output of the program?


int main()
{
int a=printf("%d ",printf("cprog\n\n"));
return 0;
}

1. cprog7
2. cprog5
3. cprog
7

4. cprog

7
Ans: option 4
Explanation: "cprog\n\n" prints cprog followed by two newlines. 7 is printed next with a space after it.

3) What will be the output of the program?


int main()
{
int arr[10],i;
for(i=0;i <= 5;i++);
arr[i]=200;
printf("%d %d",6[arr],arr[1]);
return 0;
}

1. 200 garbage value


2. Compile time error
3. Runtime error
4. garbage value 200
Ans: option 1
Explanation: a) Notice the semicolon ; at the end of the for loop. This makes the loop empty, meaning it
doesn't execute any statements. The value of i will increment from 0 to 6, and nothing else happens inside
the loop.
b)After the loop, the value of i is 6 (since the loop ran until i was incremented to 6). So this line assigns
arr[6] = 200;. The sixth index of the array arr is now 200.
c)6[arr] is equivalent to arr[6]. In C, the syntax arr[i] is the same as i[arr]. So 6[arr] is just arr[6], which has
been assigned the value 200 in the previous step. Thus, 6[arr] = 200.
d)arr[1] refers to the second element of the array, which has not been initialized, so it will contain a garbage
value.

4) What will be the output of the program?


int main()
{
int i = 3;
while(i < 4, 5)
{
printf(" c prog ");
i++;
}
printf(" welcome to c ");
return 0;
}

1. c prog
welcome to c
2. c prog infinite times
3. Compile time error
4. welcome to c
Ans: option 2
Explanation: The expression inside the while condition uses the comma operator. In C, the comma operator
evaluates the left-hand expression (i < 4), discards the result, and then evaluates the right-hand expression
(5).In this case, the right-hand expression is 5, which is a non-zero value, meaning it is always true in the
context of the while loop. Therefore, the condition while(5) is equivalent to while(true), making the loop
infinite.

5) What will be the output of the program?


int main()
{
int k=35;
printf("\n %d %d %d",k == 35,k=50,k>40);
return 0;
}

1. 1 50 0
2. 0 50 0
3. 1 35 0
4. 1 50 1
Ans: option 2
Explanation: The arguments to printf are evaluated from right to left.
 k == 35 → 0 (false, because k is now 50).
 k = 50 → 50 (assignment).
 k > 40 → 0 (false, because k was still 35 when this was evaluated).

6) What will be the output of the program?


int main()
{
int a=10,b=2;
int *pa=&a,*pb=&b;
printf("value =%d",*pa/ *pb);
return 0;
}
1. Compile time error
2.5
3. Runtime error
4. Garbage value
Ans: option 2
Explanation: *pa is a pointer to the variable a (points to the address of a).
*pb is a pointer to the variable b (points to the address of b).
*pa dereferences the pointer pa, giving the value of a, which is 10.
*pb dereferences the pointer pb, giving the value of b, which is 2.
The division operation *pa / *pb is 10 / 2, which evaluates to 5.

7) What will be the output of the program?


int main()
{
int a[4]={ [1]=10,[0]=20,[3]=40,[2]=30};
printf("%d",a[2]);
}

1.30
2.10
3. Garbage value
4.Compile time error
Ans: option 2
Explanation: int a[4] = { [1] = 10, [0] = 20, [3] = 40, [2] = 30 };
This syntax is an example of designated initializers in C, where specific indices of an array are initialized
directly. This prints the value at index 2 of the array a[]. From the initialization, we know that a[2] = 30.

8) What will be the output of the program?


char call()
{
char *ptr = "C prog";
return ptr;
}
int main()
{
printf("%s", call());
return 0;
}

1.Compile time error


2.Runtime error
3.C prog
4.Garbage value
Ans: option 1
Explanation: The function call() is declared with a return type of char, meaning it expects to return a single
char (one character).
However, the function is returning ptr, which is a pointer to a string (char *), not a single character.
This mismatch in return types will cause a compile-time error or, at least, a warning depending on the
compiler.

9) What will be the output of the program?


int main()
{
while(1)
{
if(printf("%d",printf("hello")))
break;
if(5)
continue;
}
return 0;
}

1. infinite times
2.5hello
3.hello
4.hello5
Ans: option 4
Explanation: The printf("hello") is executed first, printing the string "hello".
The return value of printf("hello") is the number of characters printed, which is 5 (since "hello" has 5
characters).
Now, printf("%d", 5) is executed. It prints 5.
The printf("%d", 5) statement returns the number of characters it printed, which is 1 (because the number 5
is a single character).

10) What will be the output of the program?


int main()
{
int array[] = {20,30,50,40,70,80};
int *p = (&array+1);
printf("%d ", *(p-1) );
return 0;
}

1.20
2.30
3.80
4.70
Ans: option 3
Explanation: &array gives the address of the entire array. Since array is of type int[6], &array is of type int
(*)[6] (pointer to an array of 6 integers).
&array + 1 moves the pointer to the memory address just past the end of the array. The pointer now points to
the address immediately after the array in memory.
p points just past the end of the array (i.e., one element beyond array[5]).
p - 1 moves the pointer back by one element, so now it points to array[5] (the last element of the array).
*(p - 1) dereferences this pointer, which gives the value of array[5], which is 80.

11) What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s\n", strcpy(str2, strcat(str1, str2)));
return 0;
}

1.Hello
2.World
3.Hello World
4.WorldHello
Ans: option 3
Explanation: Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as
an array of characters and initialized with value "Hello" and " World" respectively.
Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains
"Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.
Hence it prints "Hello World".

12) What will be the output of the program ?


#include<stdio.h>
int main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if(str1 == str2)
printf("Equal\n");
else
printf("Unequal\n");
return 0;
}

1. Equal
2. Unequal
3. Error
4. None of above
Ans: option 2
Explanation: Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and
initialized with a string "Hello".
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a
string "Hello".
We have used strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variables is not
the same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal".

13) What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
printf("%c\n", "abcdefgh"[4]);
return 0;
}

1. Error
2. d
3. e
4. abcdefgh
Ans: option 3
Explanation: printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".
Hence the output is 'e'.

14) What will be the output of the program ?


#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}

1. 2, 1, 15
2. 1, 2, 5
3. 3, 2, 15
4. 2, 3, 20
Ans: option 3
Explanation:
1. First Assignment:
i = ++a[1];
 a[1] is initially 1 (from the array initialization).
 The pre-increment operator (++a[1]) increments a[1] before the value is used.
 So, a[1] becomes 2, and the value 2 is assigned to i.
After this statement:
i=2
a[1] = 2
2. Second Assignment:
j = a[1]++;
 a[1] is currently 2 (after the previous statement).
 The post-increment operator (a[1]++) returns the current value of a[1] (which is 2) and then
increments a[1] by 1.
 So, j = 2, and then a[1] becomes 3.
After this statement:
 j=2
 a[1] = 3
3. Third Assignment:
m = a[i++];
 i is currently 2 (from the first assignment).
 The post-increment operator (i++) returns the current value of i (which is 2) and then increments i by
1.
 So, m = a[2], and i becomes 3.
 a[2] is 15 (from the array initialization), so m = 15.
After this statement:
 m = 15
 i=3

15) What will be the output of the program ?


#include<stdio.h>
int main()
{
int arr[1]={10};
printf("%d\n", 0[arr]);
return 0;
}

1. 1
2. 10
3. 0
4. 6
Ans: option 2
Explanation: Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array with size '2' and it's
first element is initialized to value '10'(means arr[0]=10)
Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr.
Hence the output of the program is 10.

16) What will be the output of the program ?


#include<stdio.h>
int main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf("%d\n", sizeof(arr)/sizeof(arr[0]));
return 0;
}
1. 5
2. 4
3. 6
4. 7
Ans: option 2
Explanation: Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point
array and it is initialized with the values.
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0]));
The variable arr has 4 elements. The size of the float variable is 4 bytes.
Hence 4 elements x 4 bytes = 16 bytes
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is '4'.

17) What will be the output of the following C code?


#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}
1.Compile time error
2. Segmentation fault/runtime crash
3. 10
4. Undefined behaviour
Ans: option 1
Explanation: You're attempting to dereference p using *p. Since p is a void *, dereferencing it without first
casting it to a proper type (such as int *) will result in a compilation error, because the compiler doesn't
know what kind of data p is pointing to.

18) What will be the output of the following C code?


#include <stdio.h>
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d\n", *ptr, a);
}
1. 10,10
2. 10,11
3. 11,10
4. 11,11
Ans: option 4
Explanation: *ptr += 1;
 This dereferences the pointer ptr, accessing the value of a through the pointer, and increments it by 1.
 The value of a becomes 11 since *ptr is referring to a, and a = 10 + 1.
printf("%d,%d/n", *ptr, a);
 This prints the value of *ptr and a.
 Since *ptr refers to a, both *ptr and a will have the value 11.

19) What will be the output of the following C code?


void main() {
char *p;
p = "Hello";
printf("%c\n", *&*p);
}
1. Hello
2. H
3. Some address will be printed
4. None of these
Ans: option 2
Explanation: * is a dereference operator & is a reference operator. They can be applied any number of times
provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so
its value is H. Again & references it to an address and * dereferences it to the value H.

20) What is the output of the following C code?


char *ptr;
char mystring[] = "abcdefg";
ptr = myString;
ptr += 5;
1. fg
2. efg
3. defg
4. cdefg
5. bcdefg
Ans: option 1
Explanation: ptr is assigned the address of the first element of mystring, so ptr initially points to 'a'.
This increments the pointer by 5 positions. After this operation, ptr points to the character at index 5 of
mystring, which is 'f'.

You might also like