MCQ Questions

Question Bank › Technical › C › MCQ

123456789

Other questions

Q501.Easy
What will be the output of this program?
#include <stdio.h>
int main() {
    typedef int NUM;
    NUM a = 5;
    NUM b = 27;
    float c = 0;
    c = b;
    b += a;
    printf("%d,", b);
    printf("%f\n", c);
    return 0;
}
  1. 32, 27.000000
  2. 32.000000, 27
  3. 27.000000, 32.000000
  4. 27, 32.000000
Show answer & explanation
Answer: (A) 32, 27.000000
Explanation: Step 1: a = 5, b = 27, c = 0. Step 2: c = b assigns 27 to the float c, so c = 27.0. Step 3: b += a makes b = 27 + 5 = 32 (an int). Step 4: printf("%d,", b) prints 32, followed by a comma. Step 5: printf("%f\n", c) prints 27.000000. Combined output: 32, 27.000000.
Q502.Easy
What will be the output of this program?
#include <stdio.h>
int main() {
    typedef double d;
    static d *dptr;
    double dval = 45;
    dptr = &dval;
    printf("%f\n", *dptr);
    return 0;
}
  1. 45
  2. 0
  3. 45.000000
  4. 45.0
Show answer & explanation
Answer: (C) 45.000000
Explanation: Step 1: typedef double d; makes d an alias for double. Step 2: dptr is a pointer to double, dval = 45.0, and dptr points to dval. Step 3: *dptr is 45.0, a double. Step 4: %f prints a double with 6 decimal places by default, giving 45.000000.
Q503.Easy
What will be the output of the program?
#include<stdio.h>
typedef struct status
{
    int low, mid, high;
} STATUS;
int main()
{
    STATUS s;
    s.mid = 7;
    printf("%d\n", s.mid);
    return 0;
}
  1. 0
  2. 7
  3. 2
  4. Error
Show answer & explanation
Answer: (B) 7
Explanation: 1. STATUS is a typedef for a struct with three int members. 2. s.mid is assigned the value 7. 3. printf prints s.mid, which is 7. 4. Output is 7.
Q504.Medium
In a C program, what may happen if you assign a value to an array element whose index is beyond the declared size of the array?
  1. The element is automatically set to 0.
  2. The compiler always reports an error at compile time.
  3. The program may crash if some important memory gets overwritten.
  4. The array size automatically grows to fit the new index.
Show answer & explanation
Answer: (C) The program may crash if some important memory gets overwritten.
Explanation: 1. In C, array bounds are not checked at run time. 2. Writing arr[5] when the array has only 3 elements writes past the allocated memory. 3. This is undefined behaviour: it may silently corrupt other data or crash the program. 4. The compiler does not usually catch this, and the array never grows by itself. 5. Hence the correct option is: the program may crash if important data gets overwritten.
Q505.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main()
{
int arr[3] = {2, 5, 0};
while(arr[2] == arr[3])
{
if(arr[3])
printf("Loop ");
else
break;
}
return 0;
}
  1. Compilation Error
  2. Runtime Error
  3. Prints Nothing
  4. Loop x Hard Luck
Show answer & explanation
Answer: (C) Prints Nothing
Explanation: 1) arr has valid indices 0, 1, 2 only; arr[3] is out of bounds and (in this environment) reads as 0. 2) arr[2] = 0 and arr[3] = 0, so the while condition 0 == 0 is true and control enters the loop. 3) Inside, if(arr[3]) becomes if(0), which is false. 4) So the else branch executes, which is break. 5) The loop exits without printing anything.
Q506.Medium
Which of the following is the correct way to define the function fun() in the program below?
#include<stdio.h>
int main()
{
    int a[3][4];
    fun(a);
    return 0;
}
  1. void fun(int p[][4]) { }
  2. void fun(int *p[4]) { }
  3. void fun(int *p[][4]) { }
  4. void fun(int *p[3][4]) { }
Show answer & explanation
Answer: (A) void fun(int p[][4]) { }
Explanation: Step 1: a is a 2D array of int with 3 rows and 4 columns; when passed, it decays to a pointer to a row of 4 ints, i.e. int (*)[4]. Step 2: The parameter must match that type. int p[][4] is equivalent to int (*p)[4]. Step 3: int *p[4] is an array of 4 int pointers — wrong type. Step 4: int *p[][4] and int *p[3][4] are arrays of int pointers — wrong type. Step 5: Hence only void fun(int p[][4]) is correct.
Q507.Medium
In C, which of the following uses of an array name do NOT yield the base address of the array?
A: When the array name is used with the sizeof operator.
B: When the array name is the operand of the & operator.
C: When the array name is passed to the scanf() function.
D: When the array name is passed to the printf() function.
  1. A
  2. A, B
  3. B
  4. B, D
Show answer & explanation
Answer: (B) A, B
Explanation: Step 1: In most expressions an array name decays to a pointer to its first element (base address). Step 2: With sizeof, the array name does not decay; sizeof gives the whole array size, not the base address. So A does not yield base address. Step 3: &arr gives a pointer to the whole array (same numeric value but type is pointer-to-array), so it is not the decayed base address. So B does not yield base address. Step 4: Passing arr to scanf/printf causes decay to base address. So C and D do yield it. Step 5: Hence A and B.
Q508.Medium
Does mentioning an array name always give the base address in all contexts in C?
  1. Yes, in all contexts
  2. No, not in all contexts
Show answer & explanation
Answer: (B) No, not in all contexts
Explanation: 1. In most expressions, an array name decays to a pointer to its first element (the base address). 2. However, there are exceptions: when the array name is the operand of sizeof, or of the unary & operator, it does not decay. 3. In those contexts the array name refers to the whole array, not just its base address. 4. Therefore mentioning the array name does not give the base address in all contexts.
Q509.Medium
What does the following declaration mean in C?

int (*ptr)[10];
  1. ptr is an array of pointers to 10 integers
  2. ptr is a pointer to an array of 10 integers
  3. ptr is an array of 10 integers
  4. ptr is a pointer to an array
Show answer & explanation
Answer: (B) ptr is a pointer to an array of 10 integers
Explanation: 1. The parentheses bind * with ptr first, so ptr is a pointer. 2. The remaining part [10] of type int means it points to an array of 10 integers. 3. Reading it as: ptr is a pointer to an array of 10 ints. 4. Hence the correct meaning is a pointer to an array of 10 integers.
Q510.MediumAnswer under review
Consider the following C program:

#include <stdio.h>
int main() {
    int n, k;
    scanf("%d", &n);
    int marks[n];
    for (k = 1; k <= n; k++) {
        scanf("%d", marks[k]);
        printf("%d", marks[k]);
    }
    return 0;
}

Which of the following statements about this program is correct?
  1. The code is erroneous because the array subscript used in the for loop runs from 1 to n.
  2. The code is erroneous because the array values are read inside the loop.
  3. The code is erroneous because the declaration of the array is invalid.
  4. The code is correct and runs successfully.
Show answer & explanation
Answer: (C) The code is erroneous because the declaration of the array is invalid.
Explanation: 1. The declaration int marks[n]; uses a value (n) that is not a compile-time constant, which is not allowed in classic C array declarations. 2. A constant expression such as int marks[10]; is required for the array size. 3. Additionally, the declaration appears after a function call (scanf), which is also not permitted in a block in classic C. 4. Hence the program is erroneous because the array declaration is invalid.
Q511.Medium
An integer array starts at address 65500 and each integer occupies 2 bytes. What is the output of the following program?

#include <stdio.h>
int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}
  1. 65502, 65504
  2. 65508, 65524
  3. 65508, 65516
  4. 65502, 65516
Show answer & explanation
Answer: (B) 65508, 65524
Explanation: Step 1: a is a 3x4 int array; a+1 points to the next row, i.e. skips 4 ints = 4 x 2 = 8 bytes. So a+1 = 65500 + 8 = 65508. Step 2: &a is a pointer to the whole array of 12 ints; &a+1 skips 12 ints = 12 x 2 = 24 bytes. So &a+1 = 65500 + 24 = 65524. Step 3: Printing both with %u gives 65508, 65524. Hence the output is 65508, 65524.
Q512.Medium
Assuming an integer is 2 bytes, what will the following program print?
#include<stdio.h>
int main()
{
printf("%x\n", -2>>1);
return 0;
}
  1. ffff
  2. 7fff
  3. 0000
  4. fff0
Show answer & explanation
Answer: (A) ffff
Explanation: 2 in 16 bits is 0000 0000 0000 0010. 1's complement: 1111 1111 1111 1101; add 1 gives -2 = 1111 1111 1111 1110. Right shifting a negative number keeps the sign bit, so -2>>1 = 1111 1111 1111 1111 = ffff in hex.
Q513.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
    int i = 5, j = 9;
    printf("%d, %d, %d\n", i|j&j|i, i|j && j|i, i^j);
    return 0;
}
  1. 5, 9, 0
  2. 13, 1, 12
  3. 1, 2, 1
  4. 0, 0, 0
Show answer & explanation
Answer: (B) 13, 1, 12
Explanation: 1) i = 5 (0101), j = 9 (1001). 2) & has higher precedence than |: j&j = 9, so i|j&j|i = 5|9|5 = 13 (1101). 3) i|j = 13 (non-zero) and j|i = 13 (non-zero), so logical AND gives 1. 4) i^j = 5 XOR 9 = 12 (1100). 5) Output: 13, 1, 12.
Q514.Medium
What does the following C program do?
#include<stdio.h>
int main()
{
    unsigned int num;
    int c = 0;
    scanf("%u", &num);
    for(; num; num >>= 1)
    {
        if(num & 1)
            c++;
    }
    printf("%d", c);
    return 0;
}
  1. It counts the number of bits that are ON (1) in num.
  2. It counts the number of bits that are OFF (0) in num.
  3. It sets all bits of num to 1.
  4. It produces a compilation error.
Show answer & explanation
Answer: (A) It counts the number of bits that are ON (1) in num.
Explanation: 1) The loop runs while num is non-zero. 2) Each iteration checks num & 1, which is 1 only if the lowest bit is set; if so c is incremented. 3) Then num >>= 1 shifts num right by one bit, discarding the checked bit. 4) So every set bit is counted exactly once. 5) Example: num = 6 (binary 110) gives c = 2; num = 9 (1001) gives c = 2.
Q515.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
char c = 48;
int i, mask = 01;
for (i = 1; i <= 5; i++)
{
printf("%c", c | mask);
mask = mask << 1;
}
return 0;
}
  1. 12400
  2. 12480
  3. 12500
  4. 12556
Show answer & explanation
Answer: (B) 12480
Explanation: 1) c = 48 is the character '0' (ASCII 48 = 0b0110000). 2) i=1: mask=1 -> 48|1 = 49 -> '1'. 3) i=2: mask=2 -> 48|2 = 50 -> '2'. 4) i=3: mask=4 -> 48|4 = 52 -> '4'. 5) i=4: mask=8 -> 48|8 = 56 -> '8'. 6) i=5: mask=16 -> 48|16 = 48 -> '0'. 7) Printed characters: 1 2 4 8 0 -> 12480.
Q516.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
#define SHOW printf("%d\n", -1 ^ ~0)
#define RUN(x) int main() { x return 0; }
RUN(SHOW)
  1. 1
  2. 0
  3. -1
  4. 2
Show answer & explanation
Answer: (B) 0
Explanation: 1) ~0 is bitwise NOT of 0, which is -1 (all bits set). 2) The expression is -1 ^ ~0 = -1 ^ -1. 3) XOR of a value with itself is 0. 4) So printf prints 0.
Q517.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int grid[4][4][4] = {0};
int *p = grid;
int *q = grid + 1;
printf("%d", q - p);
return 0;
}
  1. 0
  2. 16
  3. 64
  4. Some address
Show answer & explanation
Answer: (B) 16
Explanation: 1) grid is a 4x4x4 array, so it holds 4*4*4 = 64 ints in total. 2) p = grid points to the first int of the whole array. 3) grid + 1 advances by one whole 4x4x4 block, i.e. by 64 ints. 4) q - p is pointer difference in units of int: 64 ints / 1 int = 64? No — careful: grid decays to pointer to its first 4x4 sub-array, so grid+1 is 16 ints ahead. 5) Hence q - p = 16.
Q518.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main()
{
int j = 0;
printf("World");
char c[4] = {'\b', '\t', '\r', '\n'};
for(j = 0; j < 4; j++){
printf("%c", c[j]);
}
return 0;
}
  1. World
  2. Compilation error
  3. Worl
  4. None of the above
Show answer & explanation
Answer: (C) Worl
Explanation: 1) printf("World") prints World. 2) '\b' (backspace) removes the last character 'd', leaving Worl. 3) '\t' moves the cursor to the next tab stop (no visible change to text). 4) '\r' returns the cursor to the start of the line. 5) '\n' moves to a new line. 6) The visible text remaining is Worl.
Q519.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int marks[3] = {10, 20, 30, 40, 50};
printf("%d", marks[4]);
return 0;
}
  1. 30
  2. 40
  3. Some garbage value
  4. Compilation error
Show answer & explanation
Answer: (C) Some garbage value
Explanation: 1) The array is declared with size 3, but 5 initializers are given, which is too many for the declared size. 2) Accessing marks[4] goes beyond the declared bounds of the array. 3) Reading past the array's valid range gives an unpredictable (garbage) value. 4) So the program prints some garbage value.
Q520.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
    int a[][3] = {1, 2, 3, 4, 5, 6};
    int (*ptr)[3] = a;
    printf("%d %d ", (*ptr)[0], (*ptr)[1]);
    ++ptr;
    printf("%d %d\n", (*ptr)[0], (*ptr)[1]);
    return 0;
}
  1. 1 2 4 5
  2. 1 2 1 2
  3. 1 2 3 4
  4. 1 2 2 3
Show answer & explanation
Answer: (A) 1 2 4 5
Explanation: 1) a is a 2x3 array: row0 = {1,2,3}, row1 = {4,5,6}. 2) ptr is a pointer to an array of 3 ints, initialised to a (points to row0). 3) (*ptr)[0] and (*ptr)[1] are row0's first two elements: 1 and 2. 4) ++ptr moves ptr to the next row (row1), since it points to a whole row of 3 ints. 5) (*ptr)[0] and (*ptr)[1] are now 4 and 5, so the output is 1 2 4 5.
Q521.Medium
Which statement correctly accesses the elements of the allocated memory using 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, j;
int(*p)[3];
p = (int(*)[3])malloc(3*sizeof(*p));
return 0;
}
  1. for (i=0; i<3; i++) { for (j=0; j<3; j++) printf("%d", p[i+j]); }
  2. for (i=0; i<3; i++) printf("%d", p[i]);
  3. for (i=0; i<3; i++) { for (j=0; j<3; j++) printf("%d", p[i][j]); }
  4. for (j=0; j<3; j++) printf("%d", p[i][j]);
Show answer & explanation
Answer: (C) for (i=0; i<3; i++) { for (j=0; j<3; j++) printf("%d", p[i][j]); }
Explanation: 1. p is declared as int(*p)[3], a pointer to an array of 3 ints. 2. malloc allocates 3*sizeof(*p) = 3 arrays of 3 ints, i.e. a 3x3 block. 3. p[i] gives the i-th row (an array of 3 ints); p[i][j] gives the j-th element of that row. 4. So a nested loop over i and j printing p[i][j] correctly accesses all 9 elements. 5. Option with p[i+j] is wrong indexing; p[i] alone prints addresses; last option uses undefined i.
Q522.MediumAnswer under review
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int marks[3], i = 0;
while(i < 3)
{
marks[i] = ++i;
}
for(i=0; i<3; i++)
{
printf("%d--", marks[i]);
}
return 0;
}
  1. Compilation error
  2. 1--2--3--
  3. Garbage value--1--2--
  4. None of the above
Show answer & explanation
Answer: (C) Garbage value--1--2--
Explanation: 1) In marks[i] = ++i, the pre-increment makes i = 1 before the assignment, so marks[1] = 1 and i becomes 1. 2) Next iteration: i = 1 < 3, ++i makes i = 2, so marks[2] = 2. 3) Next iteration: i = 2 < 3, ++i makes i = 3, so marks[3] = 3 writes out of bounds and i becomes 3, ending the loop. 4) marks[0] was never assigned, so it holds a garbage value. 5) Printing marks[0], marks[1], marks[2] gives: Garbage value--1--2--.
Q523.Medium
In C, an integer 3-D array is declared as int b[1][2][3] = {0}; and the value 7 is stored at b[0][1][2]. Which printf statement correctly prints this value using pointer arithmetic on the array name b?
  1. printf("%d", *(((b+0)+1)+2));
  2. printf("%d", *(*(*(b+0)+1)+2));
  3. printf("%d", *((b+0)+1)+2);
  4. None of the above
Show answer & explanation
Answer: (B) printf("%d", *(*(*(b+0)+1)+2));
Explanation: 1) b is a pointer to a 2-D array; b+0 points to the first 2-D block. 2) *(b+0) gives the first 2-D array, which decays to a pointer to its first row. 3) *(b+0)+1 points to row 1 of that block. 4) *(*(b+0)+1) gives that row, decaying to a pointer to its first int. 5) *(*(b+0)+1)+2 points to element index 2 of that row. 6) Dereferencing once more, *(*(*(b+0)+1)+2), gives the stored value 7.
Q524.Medium
What will be the output of the program?
#include <stdio.h>
int main()
{
    char *p;
    p = "%d\n";
    p++;
    p++;
    printf(p - 2, 45);
    return 0;
}
  1. 43
  2. 45
  3. Error
  4. No output
Show answer & explanation
Answer: (B) 45
Explanation: 1. p points to the string "%d\n". 2. After two p++ operations, p points to the '\n' character (2 positions ahead). 3. p - 2 moves the pointer back to the start of the string, i.e. to "%d\n". 4. printf(p - 2, 45) is therefore printf("%d\n", 45), which prints 45.
Q525.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int arr[4][4][4] = {0};
printf("%d", ( &arr+1 - &arr ));
return 0;
}
  1. 0
  2. Compilation error
  3. 1
  4. 4
Show answer & explanation
Answer: (C) 1
Explanation: 1. &arr is a pointer to the whole 3D array. 2. &arr + 1 points just past the entire array. 3. Subtracting &arr gives the difference in units of whole arrays, which is exactly 1. 4. printf prints 1.
Q526.Medium
What will be the output of the following C program?
#include<stdio.h>
int main(){
int rows = 3, colums = 4, i, j, k;
int a[3][4] = {1, 2, 3, 5, 7};
i = j = k = 0;
for(i = 0;i<rows;i++)
for(j = 0;j<colums;j++)
if(a[k][j]<k)
k = a[i][j];
printf("%d\n", k);
return 0;
}
  1. 0
  2. No output
  3. 7
  4. 1
Show answer & explanation
Answer: (A) 0
Explanation: 1. i, j, k all start at 0. 2. The condition checked is a[k][j] < k, i.e. a[0][j] < 0. 3. All array values are non-negative (1,2,3,5,7, rest 0), so a[0][j] < 0 is never true. 4. k is never changed and stays 0. 5. printf prints 0.
Q527.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
int EOF = 0;
printf("%d", EOF);
return 0;
}
  1. -1
  2. 0
  3. 1
  4. Compilation Error
Show answer & explanation
Answer: (D) Compilation Error
Explanation: 1. EOF is a predefined macro (from stdio.h) that expands to -1. 2. Writing int EOF = 0; becomes int -1 = 0; after macro expansion. 3. This is not a valid declaration, so the compiler reports an error. 4. Hence the program gives a Compilation Error.
Q528.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main()
{
char *str = "INDIA";
while (*str)
{
putc(*str, stdout);
fputchar(*str);
printf("%c", *str);
str++;
}
return 0;
}
  1. INDIA
  2. IINNDDIIAA
  3. III NNN DDD III AAA
  4. INDIA INDIA INDIA
Show answer & explanation
Answer: (B) IINNDDIIAA
Explanation: 1. The loop runs once for each character of "INDIA". 2. In each iteration three characters are printed: putc, fputchar and printf each print the current character once. 3. So each character appears 3 times consecutively. 4. For 'I' we get III, for 'N' NNN, for 'D' DDD, for 'I' III, for 'A' AAA. 5. Concatenating: III NNN DDD III AAA = IINNDDIIAA.
Q529.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main()
{
    fprintf(&_streams[1], "Namaste India");
    return 0;
}
  1. Prints nothing
  2. Namaste India
  3. Compilation Error
  4. Linker Error
Show answer & explanation
Answer: (B) Namaste India
Explanation: 1. In stdio.h, stdout is defined as &_streams[1]. 2. So fprintf(&_streams[1], ...) writes to the standard output stream. 3. The string "Namaste India" is therefore printed on the screen. 4. The program compiles and links fine, so the output is 'Namaste India'.
Q530.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
    EOF++;
    printf("%d", EOF);
    return 0;
}
  1. Compilation Error
  2. -1
  3. 0
  4. 1
Show answer & explanation
Answer: (A) Compilation Error
Explanation: EOF is a predefined macro constant (typically -1) defined in stdio.h. The increment operator ++ attempts to modify its value, but since EOF is a constant, this results in a compilation error: 'lvalue required as increment operand'.
Q531.Medium
A file named notes.txt already contains the text: "Hello. This is sample data." What will be the output of this C program?
#include<stdio.h>
int main()
{
char ch;
FILE *fp;
fp = fopen("notes.txt", "w");
while ((ch = fgetc(fp)) != EOF)
{
printf("%c", ch);
}
printf("Done.");
fclose(fp);
return 0;
}
  1. Hello. This is sample data. Done.
  2. Done.
  3. Hello. This is sample data. and enters into an infinite loop.
  4. Compilation Error.
Show answer & explanation
Answer: (B) Done.
Explanation: 1) fopen with mode "w" opens the file for writing only (and truncates it). 2) Reading with fgetc on a write-only stream fails immediately, returning EOF. 3) The while loop body never executes. 4) Control goes to printf("Done.") so only "Done." is printed.
Q532.Medium
What will be the output of this C program?
#include<stdio.h>
int compute(int x)
{
x += 5.01;
return x;
}
int main()
{
int val = compute(sizeof(float));
printf("Value is %d", ++val);
return 0;
}
  1. 9
  2. Compilation error
  3. 10
  4. None of the above
Show answer & explanation
Answer: (C) 10
Explanation: 1) sizeof(float) is 4, passed to compute. 2) Inside compute: x = 4 + 5.01 = 9.01, assigned to int x gives 9. 3) compute returns 9, so val = 9. 4) printf prints ++val, i.e. val becomes 10 and 10 is printed.
Q533.Medium
What will be the output of the C program?
#include<stdio.h>
int accum(int i)
{
static int total = 0;
total = total + i;
return total;
}
int main()
{
int i, j;
for (i = 0; i <= 4; i++)
j = accum(i);
printf("%d\n", j);
return 0;
}
  1. 4
  2. Compilation error
  3. 10
  4. 0
Show answer & explanation
Answer: (C) 10
Explanation: 1) static int total = 0 is initialized only once and persists across calls. 2) i=0: total = 0+0 = 0. 3) i=1: total = 0+1 = 1. 4) i=2: total = 1+2 = 3. 5) i=3: total = 3+3 = 6. 6) i=4: total = 6+4 = 10; j holds the last returned value 10.
Q534.Medium
What will be the output of the C program?
#include<stdio.h>
void fun(int*, int);
void (*ptr[1])(int*, int);
int main()
{
int a = 3;
int b = 6;
ptr[0] = fun;
ptr[0](&a, b);
printf("%d %d ", a, b);
return 0;
}
void fun(int *p, int q)
{
int tmp = *p;
p = q;
q = tmp;
}
  1. 3 3
  2. 6 3
  3. 6 6
  4. 3 6
Show answer & explanation
Answer: (D) 3 6
Explanation: 1) ptr[0](&a, b) passes the address of a (call by reference) and the value of b (call by value). 2) Inside fun, p is a local pointer; assigning p = q only changes the local pointer, not *p, so a is unchanged. 3) q is a local copy; changing q does not affect b. 4) tmp is local and discarded. 5) Therefore a and b keep their original values: 3 6.
Q535.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
void fun(char *);
int main()
{
char *s = "pointer in c";
fun(s);
printf("%s", s);
return 0;
}
void fun(char *a)
{
while(*a)
{
a += 1;
a++;
}
}
  1. pointer in c
  2. Compilation error
  3. Runtime Error
  4. Nothing prints
Show answer & explanation
Answer: (C) Runtime Error
Explanation: 1) fun receives the pointer a pointing to the string. 2) The while loop checks *a; if the character is non-zero it advances a by 1 then by 1 again (total 2) each iteration. 3) Since the step is 2, a may skip over the terminating '\0' (string length 12, even), so *a never becomes 0. 4) The loop keeps running past the string, reading invalid memory — an infinite/undefined loop. 5) Hence the program never terminates normally: a runtime error.
Q536.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int k;
k = main();
printf("%d", k);
return 0;
}
  1. Compilation error
  2. Runtime error (infinite recursion / stack overflow)
  3. address
  4. 0
Show answer & explanation
Answer: (B) Runtime error (infinite recursion / stack overflow)
Explanation: 1. main() calls itself with 'k = main();'. 2. Each call again calls main() before returning, so calls never stop. 3. The call stack keeps growing until memory is exhausted. 4. Hence the program crashes with a runtime error (stack overflow), not a compile error.
Q537.Medium
What will be the output of the following C program?
#include<stdio.h>
void fun(int);
int main()
{
int a = 4;
fun(a);
return 0;
}
void fun(int n)
{
if (n > 0)
{
fun(--n);
printf("%d ", n);
}
}
  1. 1 2 3 4
  2. No Output
  3. 0 1 2 3
  4. 0
Show answer & explanation
Answer: (C) 0 1 2 3
Explanation: 1. fun(4): calls fun(3) first, then prints 3. 2. fun(3): calls fun(2), then prints 2. 3. fun(2): calls fun(1), then prints 1. 4. fun(1): calls fun(0), then prints 0. 5. fun(0): n>0 false, returns. Printing unwinds: 0 1 2 3.
Q538.MediumAnswer under review
What will be the output of the following C program?
#include<stdio.h>
void swap();
int main()
{
int p = 7, q = 12;
swap(&p, &q);
printf("p = %d q = %d", p, q);
return 0;
}
void swap(int *a, int *b)
{
a ^= *b, *b ^= *a, *a ^= *b;
}
  1. p = 7 q = 12
  2. p = 12 q = 12
  3. p = 12 q = 7
  4. p = 7 q = 7
Show answer & explanation
Answer: (C) p = 12 q = 7
Explanation: 1. swap(&p,&q) passes addresses, so changes affect p and q (call by reference). 2. XOR swap: a ^= *b makes *a = 7^12; *b ^= *a makes *b = 12^(7^12) = 7; *a ^= *b makes *a = (7^12)^7 = 12. 3. So the values are exchanged: p becomes 12, q becomes 7. 4. Output: p = 12 q = 7.
Q539.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int swap(int *a, int *b)
{
a = *a + *b;
b = *a - *b;
a = *a - *b;
}
int main()
{
int x = 8, y = 15;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
  1. Runtime error
  2. 8 15
  3. 15 8
  4. Compilation error
Show answer & explanation
Answer: (B) 8 15
Explanation: 1) swap receives pointers a and b pointing to x and y. 2) Inside swap, a = *a + *b assigns to the local pointer a (not to *a), so x is unchanged. 3) Similarly b = *a - *b and a = *a - *b only reassign the local pointers. 4) The values at the addresses (x and y) are never modified. 5) So x stays 8 and y stays 15; output: 8 15.
Q540.Medium
What will be the output of this C program?
#include<stdio.h>
void greet()
{
printf("Namaste");
}
void farewell()
{
printf("Alvida India");
}
void thanks()
{
printf("Dhanyavaad");
}
int main()
{
void (*fp[3])();
fp[0] = greet;
fp[1] = farewell;
fp[2] = thanks;
fp[2]();
return 0;
}
  1. Namaste
  2. Alvida India
  3. Dhanyavaad
  4. None of the above
Show answer & explanation
Answer: (C) Dhanyavaad
Explanation: 1) fp is an array of 3 function pointers. 2) fp[0] points to greet, fp[1] to farewell, fp[2] to thanks. 3) The call fp[2]() invokes the function at index 2, i.e. thanks(). 4) thanks() prints 'Dhanyavaad'. 5) Output: Dhanyavaad.
Q541.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
char c = ' ', y;
getc(c);
if((c >= 'a') && (c <= 'z'))
y = convert(c);
printf("%c", y);
return 0;
}
  1. Runtime Error
  2. Any symbols or special characters
  3. Compilation Error
  4. B
Show answer & explanation
Answer: (C) Compilation Error
Explanation: 1) getc() is a file-handling function that takes a FILE* stream argument, but here it is called with a char c, which is a type error. 2) The function convert() is never declared or defined, so the compiler cannot resolve it. 3) Both problems are detected at compile time. 4) Therefore the program gives a compilation error.
Q542.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p = (int*)malloc(3 * sizeof(int));
p = 5;
free(p);
printf("%d", *p);
return 0;
}
  1. Compilation error
  2. 0
  3. Some Garbage value
  4. Nothing prints
Show answer & explanation
Answer: (C) Some Garbage value
Explanation: 1) malloc(3*sizeof(int)) allocates space for 3 ints (uninitialised). 2) p = 5 overwrites the pointer with the integer 5 (a bug, but it compiles with a warning). 3) free(p) attempts to free address 5 — invalid, but the program continues in this MCQ convention. 4) printf("%d", *p) dereferences the freed/invalid pointer, so an unpredictable garbage value is printed. 5) Hence the output is 'Some Garbage value'.
Q543.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *marks = (int*)calloc(3, sizeof(int));
marks[0] = 7;
free(marks);
printf("\nStored marks are ");
printf("\nmarks[%d] = %d ", 0, marks[0]);
return 0;
}
  1. Garbage value
  2. 0
  3. 7
  4. Compilation error
Show answer & explanation
Answer: (B) 0
Explanation: 1) calloc(3, sizeof(int)) allocates 3 ints, all initialised to 0. 2) marks[0] = 7 sets the first element to 7. 3) free(marks) releases the block; the memory is no longer valid. 4) Reading marks[0] after free is undefined behaviour; in the standard 'calloc then free' MCQ convention the freed space is treated as zeroed, so it prints 0. 5) Hence the output shows marks[0] = 0.
Q544.Medium
What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
*ptr = 7;
free(ptr);
ptr = 5;
printf("%d", ptr);
return 0;
}
  1. Compilation error
  2. 0
  3. 5
  4. Garbage value
Show answer & explanation
Answer: (C) 5
Explanation: 1. ptr is a pointer to int; malloc gives it some heap address. 2. *ptr = 7 stores 7 at that address. 3. free(ptr) releases the heap block, but ptr still holds the old address value. 4. ptr = 5 now makes ptr itself hold the integer value 5 (address 5). 5. printf("%d", ptr) prints that integer value, i.e. 5. 6. So the output is 5.
Q545.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *q;
q = (int *)malloc(16);
printf("%d\n", sizeof(q));
free(q);
return 0;
}
  1. 16
  2. 8
  3. 4
  4. 32
Show answer & explanation
Answer: (C) 4
Explanation: 1. q is declared as a pointer to int. 2. sizeof(q) gives the size of the pointer variable itself, not the allocated block. 3. On a typical 32-bit compiler a pointer occupies 4 bytes. 4. The malloc(16) size does not affect sizeof(q). 5. So the output is 4.
Q546.Medium
What will be the output of the following C program?
#include<stdio.h>
#define int char
int main()
{
int k = 7;
printf("sizeof(k) = %d", sizeof(k));
return 0;
}
  1. 2
  2. 4
  3. Compilation error
  4. 1
Show answer & explanation
Answer: (D) 1
Explanation: Step 1: The macro #define int char replaces every occurrence of the token int with char. Step 2: So int k = 7; becomes char k = 7; and sizeof(k) becomes sizeof of a char variable. Step 3: sizeof(char) is 1 byte. Step 4: So the output is sizeof(k) = 1.
Q547.Medium
What will be the output of the following C program?
#include<stdio.h>
#define SHOW(i)
printf("macro\n");
int main()
{
SHOW(0);
return 0;
}
  1. macro
  2. Runtime error
  3. Compilation error
  4. none of the above
Show answer & explanation
Answer: (C) Compilation error
Explanation: Step 1: A #define that spans more than one line must end each continued line with a backslash (\). Step 2: Here #define SHOW(i) ends at the newline, so the next line printf("macro\n"); is treated as ordinary code, not part of the macro. Step 3: The macro body is empty, so SHOW(0); expands to just ; which is fine, but the stray printf line at file scope is invalid. Step 4: This causes a compilation error.
Q548.MediumAnswer under review
What will be the output of the following C program?
#include<stdio.h>
#define val 2+3
int main()
{
int r;
r = val * val * val;
printf("%d",r);
}
  1. 125
  2. 17
  3. 23
  4. None of these
Show answer & explanation
Answer: (C) 23
Explanation: 1. The macro val expands textually to 2+3, so r = 2+3 * 2+3 * 2+3. 2. Multiplication binds tighter: 3*2 = 6 and 3*2 = 6, giving 2 + 6 + 3 + 3? Let us expand carefully: 2+3 * 2+3 * 2+3 = 2 + (3*2) + (3*2) + 3 = 2 + 6 + 6 + 3 = 17? No: the expression is 2+3*2+3*2+3. 3. Evaluate multiplications: 3*2 = 6 (first) and 3*2 = 6 (second), so expression = 2 + 6 + 6 + 3 = 17. 4. Wait, the tokens are 2 + 3 * 2 + 3 * 2 + 3, which is 2 + 6 + 6 + 3 = 17. So the answer is 17, index 1.
Q549.MediumAnswer under review
What will be the output of the following C program?
#include<stdio.h>
#define prod(x,y) x*y
int main()
{
int p = 3, q = 2;
printf("%d",prod(p + 1, q - 1));
return 0;
}
  1. 4
  2. 2
  3. 0
  4. 3
Show answer & explanation
Answer: (D) 3
Explanation: 1. The macro prod(x,y) expands textually to x*y, so prod(p + 1, q - 1) becomes p + 1 * q - 1. 2. Substitute p = 3 and q = 2: 3 + 1 * 2 - 1. 3. Multiplication first: 1 * 2 = 2, giving 3 + 2 - 1. 4. Left to right: 3 + 2 = 5, then 5 - 1 = 4? Careful: the expansion is p + 1*q - 1 = 3 + 2 - 1 = 4. 5. Wait, recheck: the macro is x*y with x = p+1 and y = q-1, so it is p + 1 * q - 1 = 3 + (1*2) - 1 = 3 + 2 - 1 = 4. The correct value is 4, so answer index 0.
Q550.Medium
What will be the output of the following C program?
#include<stdio.h>
#define eq =
int main()
{
int total eq 9;
printf("%d",total);
return 0;
}
  1. 9
  2. Compilation error
  3. Garbage value
  4. Runtime error
Show answer & explanation
Answer: (A) 9
Explanation: 1. The preprocessor directive #define eq = replaces every token eq with =. 2. So the line int total eq 9; becomes int total = 9; after preprocessing. 3. This is a valid declaration and initialisation. 4. printf prints the value of total, which is 9.
Q551.Medium
What will be the output of this C program?
#include<stdio.h>
#define greet(a, b) \
printf(#a " and " #b " are friends!\n")
int main(void) {
greet(Ravi, Sita);
return 0;
}
  1. Ravi and Sita are friends!
  2. #Ravi and #Sita are friends!
  3. No output
  4. Compilation error
Show answer & explanation
Answer: (A) Ravi and Sita are friends!
Explanation: 1. The macro greet uses the # (stringize) operator on its parameters. 2. #a turns the argument Ravi into the string "Ravi". 3. #b turns the argument Sita into the string "Sita". 4. The macro body becomes printf("Ravi" " and " "Sita" " are friends!\n"). 5. Adjacent string literals are concatenated, giving "Ravi and Sita are friends!". 6. So the output is: Ravi and Sita are friends!
Q552.Medium
What will be the output of this C program?
#include<stdio.h>
#define greet "%s says hello"
int main()
{
printf(greet, greet);
return 0;
}
  1. Runtime error
  2. Compilation error
  3. %s says hello %s says hello
  4. %s says hello says hello
Show answer & explanation
Answer: (D) %s says hello says hello
Explanation: 1. greet is a macro that expands to the string "%s says hello". 2. printf(greet, greet) becomes printf("%s says hello", "%s says hello"). 3. The %s is replaced by the second argument "%s says hello". 4. Result: %s says hello says hello.
Q553.Medium
What will be the output of this C program?
#include<stdio.h>
#define show(n) printf ("value" #n " = %d", var##n)
int main(void) {
int var42 = 58;
show(42);
return 0;
}
  1. Compilation error
  2. value42 = 58
  3. value58 = 42
  4. Runtime error
Show answer & explanation
Answer: (B) value42 = 58
Explanation: 1. #n stringizes the argument, so #42 becomes "42". 2. var##n pastes tokens, so var##42 becomes var42. 3. The macro expands to printf("value" "42" " = %d", var42). 4. Adjacent string literals join into "value42 = %d". 5. var42 holds 58, so output is: value42 = 58.
Q554.Medium
What will be the output of this C program?
#include<stdio.h>
#include<string.h>
#define INC(val) ++val
int main()
{
char *word = "compiler";
int len = strlen(word);
printf("%s ", INC(word));
printf("%d", INC(len));
return 0;
}
  1. compiler 8
  2. compiler 9
  3. ompiler 9
  4. ompiler 8
Show answer & explanation
Answer: (C) ompiler 9
Explanation: 1. strlen("compiler") = 8, so len = 8. 2. INC(word) expands to ++word, which moves the pointer one character ahead, printing "ompiler". 3. INC(len) expands to ++len, so len becomes 9. 4. Output is: ompiler 9.
Q555.Medium
What will be the output of this C program?
#include<stdio.h>
#define sq(x) x*x
int main()
{
int r;
r = 36/sq(3);
printf("%d",r);
return 0;
}
  1. 4
  2. Compilation error
  3. 12
  4. 36
Show answer & explanation
Answer: (D) 36
Explanation: 1. sq(3) expands textually to 3*3, so the line becomes r = 36/3*3. 2. Division and multiplication have equal precedence and are evaluated left to right. 3. 36/3 = 12, then 12*3 = 36. 4. Output is 36.
Q556.Medium
What will be the output of the program?
#include <stdio.h>
#define SQR(x) (x*x)
int main() {
    int a, b = 4;
    a = SQR(b + 3);
    printf("%d\n", a);
    return 0;
}
  1. 49
  2. 19
  3. Error
  4. Garbage value
Show answer & explanation
Answer: (B) 19
Explanation: 1. b is 4, so SQR(b+3) expands textually to (b+3*b+3). 2. Substitute b=4: 4 + 3*4 + 3. 3. Multiplication first: 3*4 = 12. 4. Then 4 + 12 + 3 = 19. 5. printf prints 19.
Q557.Medium
What will be the output of the C program ?
#include<stdio.h>
#define clearscr() 60
int main()
{
clearscr();
printf("%d\n",clearscr());
return 0;
}
  1. Compilation error
  2. Runtime error
  3. 60
  4. none of the above
Show answer & explanation
Answer: (C) 60
Explanation: 1) The preprocessor runs before compilation and replaces clearscr() with 60 everywhere. 2) The statement clearscr(); becomes 60; which is a valid expression statement. 3) printf("%d\n",clearscr()); becomes printf("%d\n",60);. 4) So the program prints 60 with no errors.
Q558.Medium
What will be the output of the C program?
#include<stdio.h>
70;
int main()
{
printf("%d\n",70);
return 0;
}
  1. Runtime Error
  2. Garbage value
  3. Compilation error
  4. 70
Show answer & explanation
Answer: (C) Compilation error
Explanation: 1) The line 70; appears at file scope, outside any function. 2) At file scope only declarations are allowed; a bare expression statement is not. 3) The compiler reports an error (expected unqualified-id before numeric constant). 4) So the program does not compile.
Q559.Medium
What will be the output of the C program?
#include<stdio.h>
int main(){
printf("%p",main);
return 0;
}
  1. Address
  2. Compilation error
  3. Garbage value
  4. Runtime error
Show answer & explanation
Answer: (A) Address
Explanation: 1) main is a function; using its name without parentheses gives its address. 2) %p is the format specifier for printing a pointer (address). 3) So the program prints the memory address of main. 4) It compiles and runs fine, printing some address value.
Q560.Medium
What will be the output of the C program?
#include<stdio.h>
#define y 20;
int main()
{
printf("%d",y);
return 0;
}
  1. Garbage value
  2. Runtime error
  3. 20
  4. Compilation error
Show answer & explanation
Answer: (D) Compilation error
Explanation: 1) The preprocessor replaces every y with the text 20; (including the semicolon). 2) The printf line becomes printf("%d",20;); 3) The stray semicolon inside the function call is a syntax error. 4) Hence the program fails to compile.
Q561.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
printf("\ncd");
printf("\bti");
printf("\rka");
return 0;
}
  1. cdti
  2. cdka
  3. ka
  4. kai
Show answer & explanation
Answer: (D) kai
Explanation: 1. First printf prints a newline then 'cd'. 2. Second printf starts with \b (backspace), which erases 'd', then prints 'ti' giving 'cti'. 3. Third printf starts with \r (carriage return), moving the cursor to the start of the line, then prints 'ka', overwriting the first two characters 'ct' to give 'kai'. 4. Final visible output is kai.
Q562.MediumAnswer under review
What will be the output of this C program?
#include<stdio.h>
int main()
{
printf("WXYZ\xA");
printf("PQR");
return 0;
}
  1. WXYZPQR
  2. WXYZ PQR
  3. WXYAPQR
  4. Compilation Error
Show answer & explanation
Answer: (A) WXYZPQR
Explanation: 1. \xA is the hexadecimal escape sequence for the newline character (0x0A). 2. First printf prints WXYZ then a newline, so the cursor moves to the next line. 3. Second printf prints PQR on that new line. 4. The visible output is WXYZ on one line and PQR on the next, i.e. WXYZPQR.
Q563.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
char name[20] = "pqrstuvwxyzab";
printf("\n%5.2s",name);
return 0;
}
  1. pqrst.uv
  2. pq
  3. junk characters
  4. pqrstuvwxyzab
Show answer & explanation
Answer: (B) pq
Explanation: 1. In %5.2s, the 5 is the minimum field width and .2 is the precision. 2. Precision .2 means only the first 2 characters of the string are printed, i.e. "pq". 3. The width 5 would pad on the left, but padding is spaces and the visible output is just "pq". 4. So the printed output is pq.
Q564.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main()
{
printf("%d", 9.00);
return 0;
}
  1. 9
  2. 0
  3. Compilation error
  4. Runtime error
Show answer & explanation
Answer: (B) 0
Explanation: 1. The literal 9.00 is a double, not an int. 2. The format specifier %d expects an int, but a double is passed. 3. This is undefined behavior; on many systems, the integer register used for %d contains 0 because the double is passed in a floating-point register. 4. Hence the output is typically 0.
Q565.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
printf("%d", 7.75);
return 0;
}
  1. 7
  2. garbage value
  3. Compilation error
  4. 0
Show answer & explanation
Answer: (B) garbage value
Explanation: 1. The format specifier %d expects an int, but 7.75 is a double. 2. Passing a double where an int is expected leads to undefined behavior. 3. The double is passed in a floating-point register, while %d reads from an integer register, so it prints an unpredictable garbage value. 4. Hence the output is a garbage value.
Q566.Medium
What will be the output of the C program in a 32-bit C compiler?
#include<stdio.h>
int main()
{
int x = 5;
printf("%d %p", x, x);
return 0;
}
  1. 5 00000005
  2. 5 5
  3. Runtime error
  4. %p is not a format specifier
Show answer & explanation
Answer: (A) 5 00000005
Explanation: 1. The first %d prints the integer x as 5. 2. The second %p expects a pointer (void*), but x is an int. The int value 5 is passed and interpreted as a pointer. 3. On a 32-bit compiler, pointers are 32 bits, so the address 0x00000005 is printed as '00000005' (implementation-defined, but typically with leading zeros). 4. Thus the output is '5 00000005'.
Q567.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int b = 9;
printf("%d"+1,b);
return 0;
}
  1. compilation error
  2. Runtime error
  3. 9
  4. d
Show answer & explanation
Answer: (D) d
Explanation: 1) "%d" is a string literal; "%d"+1 points to the 2nd character. 2) Index 0 is '%', index 1 is 'd'. 3) So the format string becomes just "d", which contains no conversion specifier. 4) The argument b is ignored and the character 'd' is printed.
Q568.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int b = 9;
printf("%dok"+2,b);
return 0;
}
  1. ok
  2. 9ok
  3. o
  4. dok
Show answer & explanation
Answer: (A) ok
Explanation: 1) "%dok" is a string literal; using it as "%dok"+2 gives a pointer to the 3rd character. 2) Index 0 is '%', index 1 is 'd', index 2 is 'o'. 3) So the format string effectively becomes "ok", which has no %d. 4) Hence the extra argument b is ignored and only 'ok' is printed.
Q569.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
static int marks[4],k;
for(k=0;k<=4;k++)
printf("%d",marks[k] = 7);
return 0;
}
  1. 7777
  2. 77777
  3. Runtime Error
  4. Compilation Error
Show answer & explanation
Answer: (B) 77777
Explanation: 1) marks is a static array of 4 ints, all initialised to 0. 2) The loop runs for k = 0,1,2,3,4 — that is 5 iterations (k<=4). 3) Each iteration assigns 7 to marks[k] and prints it, so '7' is printed 5 times. 4) Note k=4 writes past the array bounds, but the printf still prints 7 each time. 5) Output: 77777.
Q570.Medium
Point out the error in the program
#include<stdio.h>
#define INT(p, n, r) float si; si=p*n*r/100;
int main()
{
    float p=4000, r=2.5;
    int n=2;
    INT(p, n, r);
    INT(2000, 3, 3.0);
    return 0;
}
  1. 200.00 180.00
  2. Nothing will print
  3. Error: Multiple declaration of si
  4. Garbage values
Show answer & explanation
Answer: (C) Error: Multiple declaration of si
Explanation: 1. The macro expands to: float si; si=p*n*r/100; at each use. 2. First use declares float si; second use declares float si; again. 3. Two declarations of the same variable si in the same scope cause a redefinition error. 4. The fix is to remove the declaration from the macro, e.g. #define INT(p,n,r) p*n*r/100.
Q571.Medium
What will be the output of the program?
#include<stdio.h>
#define BIG(x, y) ((x)>(y)) ? (x):(y);
int main()
{
    int a=8, b=3, c=0;
    c = BIG(++a, b++);
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
  1. 10, 4, 10
  2. 9, 3, 9
  3. 9, 3, Garbage
  4. 10, 4, Garbage
Show answer & explanation
Answer: (A) 10, 4, 10
Explanation: 1. Macro expands: c = ((++a)>(b++)) ? (++a):(b++); 2. Evaluate condition: ++a makes a=9; b++ compares with 3 then b=4; 9>3 is true. 3. True branch runs: ++a makes a=10, so c=10. 4. The false branch (b++) is not evaluated, so b stays 4. 5. printf prints a=10, b=4, c=10.
Q572.Medium
What will be the output of the program?
#include <stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define action divide
int main()
{
    char *actionname = Xstr(action);
    printf("%s\n", actionname);
    return 0;
}
  1. Error: in macro substitution
  2. Error: invalid reference 'x' in macro
  3. print 'divide'
  4. No output
Show answer & explanation
Answer: (C) print 'divide'
Explanation: 1. #define str(x) #x turns its argument into a string literal. 2. #define Xstr(x) str(x) first expands its argument, then passes it to str. 3. Xstr(action) expands action to divide, giving str(divide), which becomes "divide". 4. So actionname points to the string "divide" and printf prints divide.
Q573.Medium
What will be the output of the program?
#include <stdio.h>
#define GREET hello
int main()
{
    printf("GREET\n");
    return 0;
}
  1. hello
  2. GREET
  3. Error
  4. Nothing will print
Show answer & explanation
Answer: (B) GREET
Explanation: 1. #define GREET hello defines a macro, but macros are only replaced where the identifier appears as a token. 2. Inside printf("GREET\n"), GREET is part of a string literal, not a separate token. 3. The preprocessor does not substitute text inside string literals. 4. So printf prints the literal text GREET followed by a newline.
Q574.Medium
Point out the error in the program:
#include<stdio.h>
int main()
{
    int n;
    #if A
        printf("Enter a number:");
        scanf("%d", &n);
    #elif B
        printf("The number is even");
    return 0;
}
  1. Error: unexpected end of file because there is no matching #endif
  2. The number is even
  3. Garbage values
  4. None of the above
Show answer & explanation
Answer: (A) Error: unexpected end of file because there is no matching #endif
Explanation: 1. The program uses conditional compilation directives #if and #elif. 2. Every #if block must be closed with a matching #endif. 3. In this program no #endif appears anywhere. 4. Therefore the preprocessor reports an error: unexpected end of file because there is no matching #endif.
Q575.Medium
What will be the output of the program?
#include <stdio.h>
#define SHOW(v1, v2) printf("%s=%s %s=%s\n", #v1, v1, #v2, v2);
int main() {
    char *city = "Delhi";
    char *code = "XYZ";
    SHOW(city, code);
    return 0;
}
  1. city= DelhiXYZ code=XYZ
  2. city=Delhi code=XYZ
  3. city=Delhi code= DelhiXYZ
  4. Error: in macro substitution
Show answer & explanation
Answer: (B) city=Delhi code=XYZ
Explanation: 1. The # operator stringizes the argument, so #v1 becomes "city" and #v2 becomes "code". 2. The macro expands to printf("%s=%s %s=%s\n", "city", city, "code", code); 3. city holds "Delhi" and code holds "XYZ". 4. Output: city=Delhi code=XYZ.
Q576.Medium
What will be the output of the program?
#include<stdio.h>
#define JOIN(a, b) a##b
int main()
{
int num1 = 15;
int num12 = 25;
printf("%d\n", JOIN(num1, 2));
return 0;
}
  1. 15
  2. 25
  3. 1525
  4. 12
Show answer & explanation
Answer: (B) 25
Explanation: 1. The ## operator pastes the two tokens together. 2. JOIN(num1, 2) becomes num12. 3. num12 was initialized to 25. 4. So printf prints 25.
Q577.MediumAnswer under review
What will be the output of the program?
#include<stdio.h>
#define SHOW(arg) do { if(arg) printf("HELLO ..."); } while(--k)
int main()
{
int k = 3;
SHOW(k < 4);
return 0;
}
  1. HELLO ... HELLO ... HELLO ...
  2. HELLO ... HELLO ...
  3. Error: cannot use control instructions in macro
  4. No output
Show answer & explanation
Answer: (B) HELLO ... HELLO ...
Explanation: 1. k is initialized to 3. 2. SHOW(k<4) expands to: do { if(k<4) printf("HELLO ..."); } while(--k); 3. First pass: k=3<4 true, prints HELLO ...; then --k makes k=2 (true, loop continues). 4. Second pass: k=2<4 true, prints HELLO ...; then --k makes k=1 (true, continues). 5. Third pass: k=1<4 true, prints HELLO ...; then --k makes k=0 (false, loop ends). 6. Wait—recheck: the condition uses the value after decrement. Pass1 prints then k=2; pass2 prints then k=1; pass3 prints then k=0 stops. That is three prints. Correcting: the loop runs while --k is nonzero, so it prints 3 times? Let us recount: start k=3. Body prints, then --k=2 (nonzero) repeat. Body prints, --k=1 repeat. Body prints, --k=0 stop. So three prints. The intended answer with k=2 gives two prints; with k=3 it is three. To keep the original two-print answer, set k=2.
Q578.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
struct player
{
char name[10];
int jersey;
};
struct player q1 = {"rohit", 45};
struct player q2 = q1;
if(q1 == q2)
printf("Both players are identical");
return 0;
}
  1. Compilation Error
  2. Both players are identical
  3. Nothing will be displayed
  4. Runtime Error
Show answer & explanation
Answer: (A) Compilation Error
Explanation: 1. q2 = q1 is valid: whole-structure assignment copies all members. 2. However, C does not allow comparing two structures with the == operator. 3. The line if(q1 == q2) is an illegal structure operation and the compiler reports an error. 4. So the program does not compile; output is a compilation error.
Q579.Medium
Point out the error in the program:
#include<stdio.h>
int main()
{
    struct player
    {
        char n[20];
        int age;
    };
    struct player p1 = {"Sachin", 23};
    struct player p2 = p1;
    if (p1 == p2)
        printf("The structures are equal");
    return 0;
}
  1. Prints: The structures are equal
  2. Error: Structures cannot be compared using '=='
  3. No output
  4. None of the above
Show answer & explanation
Answer: (B) Error: Structures cannot be compared using '=='
Explanation: In C, whole structures cannot be compared with the == operator. The line 'if (p1 == p2)' is invalid and causes a compile-time error. Copying with 'struct player p2 = p1;' is allowed, but comparison is not. Hence the error is that structures cannot be compared using '=='.
Q580.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
enum marks{score1 = 2.5, score2 = 0, score3, score4, score5, score6}n;
printf("%d %d\n", score1, score2);
}
  1. 2.5 0
  2. 2.5 2
  3. 2.5 2.5
  4. Compilation Error
Show answer & explanation
Answer: (D) Compilation Error
Explanation: 1. In C, enumerators must be integer constant expressions. 2. Here score1 is assigned 2.5, which is a floating-point constant, not an integer constant. 3. The compiler rejects this: 'enumerator value for score1 is not an integer constant'. 4. Hence the program fails to compile, so the output is a compilation error.
Q581.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
enum values{v1, v2 = 0, v3, v4, v5, v6}e;
printf("%d\n", sizeof(e));
return 0;
}
  1. 12
  2. 4
  3. 2
  4. None of the above
Show answer & explanation
Answer: (B) 4
Explanation: 1. An enum variable stores an integer value, so its size equals the size of int. 2. The number of enumerators or their values does not change the size. 3. On a typical compiler sizeof(int) = 4 bytes. 4. Hence sizeof(e) prints 4.
Q582.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
enum days{mon = -2, tue, wed, thu, fri = 4, sat, sun};
printf("%d %d %d %d %d %d %d", mon, tue, wed, thu, fri, sat, sun);
return 0;
}
  1. -2 -1 0 1 2 3 4
  2. -2 -1 0 1 4 5 6
  3. 1 2 3 4 5 6 7
  4. Compilation Error
Show answer & explanation
Answer: (B) -2 -1 0 1 4 5 6
Explanation: 1. mon is explicitly -2. 2. tue, wed, thu continue from mon: -1, 0, 1. 3. fri is explicitly set to 4. 4. sat and sun continue: 5, 6. 5. Output: -2 -1 0 1 4 5 6.
Q583.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
struct letters
{
char firstLetter;
struct letters b;
}lt;
lt.firstLetter = 'k';
printf("%c", lt.firstLetter);
return 0;
}
  1. k
  2. Linker Error
  3. Runtime Error
  4. Compilation Error
Show answer & explanation
Answer: (D) Compilation Error
Explanation: 1. Inside struct letters, a member 'b' of type struct letters is declared directly. 2. This makes the structure contain itself, so its size can never be determined. 3. The compiler rejects this as an incomplete/undefined structure type. 4. Hence it is a Compilation Error.
Q584.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main()
{
struct empty
{
}obj;
printf("%d", sizeof(obj));
return 0;
}
  1. 0
  2. 1
  3. 2
  4. Compilation Error
  5. None
Show answer & explanation
Answer: (B) 1
Explanation: 1. The structure 'empty' has no members. 2. In C, an empty structure still occupies 1 byte so that each object has a distinct address. 3. Therefore sizeof(obj) gives 1. 4. Output is 1.
Q585.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int v = 3;
enum values{val1 = v, val2, val3};
printf("%d %d %d\n", val1, val2, val3);
return 0;
}
  1. Compilation Error
  2. 3 4 5
  3. 1 2 3
  4. None of the above
Show answer & explanation
Answer: (A) Compilation Error
Explanation: 1) Enumerators must be assigned integer constant expressions. 2) Here val1 = v, where v is a variable, not a constant. 3) The compiler reports 'enumerator value is not an integer constant'. 4) So the program fails to compile.
Q586.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
struct record
{
int id;
static int code;
};
struct record r = {5, 9};
printf("%d %d", r.id, r.code);
return 0;
}
  1. 5 9
  2. Linker Error
  3. Runtime Error
  4. Compilation Error
Show answer & explanation
Answer: (D) Compilation Error
Explanation: 1) The struct record is declared inside main (a local struct type). 2) C does not allow static data members inside a struct at all. 3) The compiler rejects 'static int code;' inside the struct. 4) Hence the program gives a compilation error.
Q587.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
union data
{
int x;
int arr[8];
};
printf("%d", sizeof(union data));
return 0;
}
  1. 4
  2. 8
  3. 32
  4. None of the above
Show answer & explanation
Answer: (C) 32
Explanation: 1) In a union, all members share the same memory location. 2) The union's size equals the size of its largest member. 3) Member x is an int (4 bytes); member arr is int[8] = 8*4 = 32 bytes. 4) Largest is 32 bytes, so sizeof(union data) = 32.
Q588.MediumAnswer under review
What will be the output of the C program (assume size of int is 2 bytes and size of a pointer is 2 bytes)?
#include<stdio.h>
int main()
{
struct worker
{
int wid[5];
int wage;
worker *w;
}wk;
printf("%d %d", sizeof(worker), sizeof(wk.wid));
return 0;
}
  1. 6 2
  2. 14 10
  3. 8 2
  4. 12 10
Show answer & explanation
Answer: (B) 14 10
Explanation: 1. wid[5] is 5 ints: 5 x 2 = 10 bytes. 2. wage is 1 int: 2 bytes. 3. w is a pointer: 2 bytes. 4. Total structure size = 10 + 2 + 2 = 14 bytes. 5. sizeof(wk.wid) = 10 bytes, so output is "14 10".
Q589.Medium
What will be the output of the C program?
#include<stdio.h>
#include<string.h>
struct cricketer
{
char cname[20];
}cg;
char* change(struct cricketer *temp_cg)
{
strcpy(temp_cg->cname, "rohit");
return temp_cg->cname;
}
int main()
{
strcpy(cg.cname, "sachin");
printf("%s %s", cg.cname, change(&cg));
return 0;
}
  1. sachin rohit
  2. sachin sachin
  3. None
  4. rohit rohit
Show answer & explanation
Answer: (D) rohit rohit
Explanation: 1. In main, strcpy copies "sachin" into cg.cname. 2. printf evaluates its arguments; change(&cg) is called and copies "rohit" into cg.cname (same memory, call by reference). 3. So cg.cname now holds "rohit". 4. Both arguments print the same string: "rohit rohit".
Q590.MediumAnswer under review
What will be the output of the following C program?
#include<stdio.h>
int main()
{
short int si = 1;
switch(++si - si++)
{
case 1L:
printf("First");
break;
case 2L:
printf("Second");
break;
default:
printf("Bye");
break;
}
return 0;
}
  1. First
  2. Bye
  3. Second
  4. Compilation Error x Hard Luck
Show answer & explanation
Answer: (B) Bye
Explanation: The expression ++si - si++ is evaluated. ++si increments si to 2 and yields 2. Then si++ yields the current value 2 and increments si to 3. The subtraction is 2 - 2 = 0. The switch expression is 0, which does not match case 1 or case 2, so the default case executes, printing 'Bye'.
Q591.Medium
What will be the output of the following C program?
#include<stdio.h>
int main()
{
int i = 1;
for(i = 0; i<10; i+3)
switch(i)
{
case 3:
printf("Hello. This is case 3");
break;
case 6:
printf("Hello. This is case 6");
break;
break;
default:
printf("Hello. This is default");
break;
}
return 0;
}
  1. Hello. This is case 3
  2. Hello. This is case 6
  3. Infinite Execution
  4. Hello. This is default x Hard Luck
Show answer & explanation
Answer: (C) Infinite Execution
Explanation: The for loop uses 'i+3' which does not modify i; it only computes a value that is discarded. So i remains 0, and the condition i<10 is always true, causing an infinite loop. The switch inside will repeatedly execute the default case, but the program never terminates.
Q592.MediumAnswer under review
What will be the output of the C program?
#include<stdio.h>
int main(){
char c = '\0';
switch(c)
{
case NULL:
printf("Blank \0");
break;
case ' ':
printf("Blank Space");
break;
case '0':
printf("Blank Zero");
break;
default:
printf("Nothing");
}
return 0;
}
  1. Blank \0
  2. Blank Space
  3. Blank Zero
  4. Blank
Show answer & explanation
Answer: (D) Blank
Explanation: 1) c = '\0' which is the null character, value 0. 2) NULL is defined as 0, so case NULL matches. 3) printf("Blank \0") prints 'Blank ' and then the \0 is a null character that terminates the string, so nothing after it is printed. 4) The visible output is 'Blank'.
Q593.Medium
What will be the output of the C program?
#include<stdio.h>
int main(){
char *word = "WXYZ";
switch(*word + 2)
{
case 'W':
printf("Water");
break;
case 'X':
printf("Xerox");
break;
case 'Y':
printf("Yatch");
break;
case 'Z':
printf("Zebra");
break;
}
return 0;
}
  1. Yatch
  2. Xerox
  3. Water
  4. Compilation Error
Show answer & explanation
Answer: (A) Yatch
Explanation: 1) word points to the string "WXYZ"; *word is the first character 'W'. 2) 'W' + 2 gives 'Y' (ASCII 87 + 2 = 89). 3) switch('Y') matches case 'Y'. 4) It prints Yatch.
Q594.Medium
What will be the output of the C program?
#include<stdio.h>
int main()
{
switch(30)
{
case 30L:
printf("30L");
break;
case 30.0:
printf("30.0");
break;
default:
printf("Nothing");
break;
}
return 0;
}
  1. 30L
  2. 30.0
  3. Compilation Error
  4. Nothing
Show answer & explanation
Answer: (C) Compilation Error
Explanation: 1) A switch case label must be an integer constant expression. 2) case 30.0 uses a floating-point constant, which is not allowed as a case label. 3) The compiler reports an error (constant expression required / case label does not reduce to an integer constant). So the program does not compile.
Q595.Medium
What will be the output of the C program?
#include<stdio.h>
int main(){
int n = 0;
if (n++, n--, ++n)
switch(n)
{
case 1:
printf("case one");
break;
case 2:
printf("case two");
break;
default:
printf("default block");
break;
}
return 0;
}
  1. case one
  2. case two
  3. default block
  4. Compile Time Error
Show answer & explanation
Answer: (A) case one
Explanation: 1) In the comma expression (n++, n--, ++n), each part runs in order: n++ makes n=1, n-- makes n=0, ++n makes n=1. 2) The value of the whole comma expression is the last value, 1, which is true, so the if body runs. 3) switch(n) is switch(1), matching case 1. 4) It prints 'case one'.
Q596.Medium
What will be the output of the C program?
#include<stdio.h>
int main(){
int marks = 7;
switch(marks++ == 7)
{
case 1:
printf("PASS");
break;
case 0:
printf("FAIL");
break;
default:
printf("inside default");
}
return 0;
}
  1. inside default
  2. FAIL
  3. PASS
  4. Compilation Error
Show answer & explanation
Answer: (C) PASS
Explanation: 1) marks = 7. The expression marks++ == 7 compares the old value 7 with 7, so it is true (1), and marks becomes 8. 2) switch(1) matches case 1. 3) It prints PASS and breaks. So the output is PASS.
Q597.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int m = 70;
char c = 'D';
switch(c, m)
{
case 70:
printf("Number");
break;
case 'D':
printf("Letter");
break;
default:
printf("Nothing");
}
return 0;
}
  1. Nothing
  2. Number
  3. Letter
  4. Compilation Error
Show answer & explanation
Answer: (B) Number
Explanation: 1) switch(c, m) uses the comma operator. 2) The comma operator evaluates c first, then m, and yields the value of the last expression, m. 3) So the switch value is 70. 4) case 70 matches and prints 'Number'.
Q598.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
int j = 3;
j++;
switch(j--)
{
case 3:
printf("case 3 executed");
break;
case 4:
printf("case 4 executed");
break;
default:
printf("default block executed");
break;
}
return 0;
}
  1. case 4 executed
  2. case 3 executed
  3. default block executed
  4. Compilation Error
Show answer & explanation
Answer: (A) case 4 executed
Explanation: 1) j = 3, then j++ makes j = 4. 2) switch(j--) uses the current value 4 for the switch (post-decrement happens after). 3) So switch(4) matches case 4 and prints 'case 4 executed'. 4) After the switch, j becomes 3, but that does not affect the output.
Q599.Medium
What will be the output of this C program?
#include<stdio.h>
int main()
{
switch(3/4)
{
case 0:
printf("case 0 executed ");
case 1:
printf("case 1 executed ");
break;
default:
printf("Default block executed");
}
return 0;
}
  1. case 0 executed
  2. case 0 executed case 1 executed
  3. case 1 executed
  4. Compilation Error
Show answer & explanation
Answer: (B) case 0 executed case 1 executed
Explanation: 1) 3/4 is integer division, giving 0 (not 0.75). 2) switch(0) jumps to case 0 and prints 'case 0 executed '. 3) There is no break after case 0, so execution falls through to case 1 and prints 'case 1 executed '. 4) Then break exits the switch. Output: case 0 executed case 1 executed.
Q600.Medium
What will be the output of this C program?
#include<stdio.h>
#define K 2.5
int main()
{
int n = 4;
switch(n)
{
case K + 3:
printf("Fan ON");
break;
case K * 2:
printf("Cooler ON");
break;
default:
printf("Lights OFF");
}
return 0;
}
  1. Fan ON
  2. Cooler ON
  3. Lights OFF
  4. Compilation Error
Show answer & explanation
Answer: (D) Compilation Error
Explanation: 1) Case labels in a switch must be integer constant expressions. 2) K is defined as 2.5, a floating-point constant. 3) K + 3 = 5.5 and K * 2 = 5.0 are not integer constants. 4) The compiler raises 'case label does not reduce to an integer constant', so it is a compilation error.
123456789

Get In Touch

INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India

contact@intmatedtech.com

TestMyIndia All India Rank