C Programming MCQ
C Programming MCQ
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.
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.
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.
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).
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.
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).
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.
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".
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".
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'.
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
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.