What is the output of this C program?
#include <stdio.h>
int main() {
int x = 5;
int y = x++;
printf("%d %d", y, x);
return 0;
}Solution
Post-increment returns the old value (5), then x becomes 6.
20 technical questions of moderate difficulty, as asked in campus placement and company tests. Each has a short explanation.
What is the output of this C program?
#include <stdio.h>
int main() {
int x = 5;
int y = x++;
printf("%d %d", y, x);
return 0;
}Post-increment returns the old value (5), then x becomes 6.
What is the output of this C program?
#include <stdio.h>
int main() {
int a = 5, b = 2;
printf("%d", a / b);
return 0;
}Both operands are integers, so 5 / 2 is integer division: 2.
What is the output of this C program?
#include <stdio.h>
int main() {
printf("%d", 7 % -3);
return 0;
}In C, the sign of a % b follows the dividend: 7 % -3 = 1.
What is the output of this C program?
#include <stdio.h>
void swap(int x, int y) { int t = x; x = y; y = t; }
int main() {
int a = 10, b = 20;
swap(a, b);
printf("%d %d", a, b);
return 0;
}The function swaps copies of the values (call by value), so a and b in main are unchanged.
What is the output of this C program?
#include <stdio.h>
void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; }
int main() {
int a = 10, b = 20;
swap(&a, &b);
printf("%d %d", a, b);
return 0;
}Pointers to a and b are passed, so the function swaps the actual variables.
What is the output of this C program?
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d", *(p + 2));
return 0;
}p points to arr[0]; p + 2 points to arr[2], which is 30.
What is the output of this C program?
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello";
printf("%zu", strlen(s));
return 0;
}strlen counts characters before the terminating '\0': "Hello" has 5.
What is the output of this C program?
#include <stdio.h>
int main() {
char s[] = "Hello";
printf("%zu", sizeof(s));
return 0;
}sizeof includes the terminating '\0': "Hello" occupies 6 bytes.
What is the output of this C program?
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 3) break;
printf("%d ", i);
}
return 0;
}The loop breaks when i becomes 3, before printing it.
What is the output of this C program?
#include <stdio.h>
int main() {
for (int i = 0; i <= 5; i++) {
if (i % 2 == 0) continue;
printf("%d ", i);
}
return 0;
}continue skips the even values; only odd i from 0 to 5 are printed.
What is the output of this C program?
#include <stdio.h>
int main() {
int n = 2;
switch (n) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
case 3: printf("Three"); break;
}
return 0;
}case 2 runs and break stops the fall-through.
What is the output of this C program?
#include <stdio.h>
int main() {
int n = 2;
switch (n) {
case 1: printf("One");
case 2: printf("Two");
case 3: printf("Three");
}
return 0;
}Without break, execution falls through from case 2 into case 3.
What is the output of this C program?
#include <stdio.h>
int f(int n) { return n <= 1 ? 1 : n * f(n - 1); }
int main() {
printf("%d", f(5));
return 0;
}The function computes 5! = 120 recursively.
What is the output of this C program?
#include <stdio.h>
void count() { static int c = 0; c++; printf("%d ", c); }
int main() {
count(); count(); count();
return 0;
}A static local variable keeps its value between calls.
Which function is used to allocate memory dynamically and initialise it to zero in C?
calloc() allocates memory for an array and sets all bytes to zero; malloc() does not initialise.
What is the size of a pointer on a typical 64-bit system?
On 64-bit systems, addresses are 64 bits, so pointers take 8 bytes.
Which storage class makes a global variable visible only within its own file?
A static global variable has internal linkage: it is visible only in its own source file.
What does the following declaration mean? int *p[5];
[] binds tighter than *, so p is an array of 5 elements, each a pointer to int.
What is the output of this C program?
#include <stdio.h>
int main() {
int x;
x = (2, 3);
printf("%d", x);
return 0;
}The comma operator evaluates both sides and returns the right one; the parentheses make x = (2, 3) = 3.
What is the output of this C program?
#include <stdio.h>
int main() {
int c = 97;
printf("%c", c);
return 0;
}%c prints the character whose code is 97, which is 'a'.
Want guided preparation? Placement Papers Prep courses by Dr. Kiran Derle, with video lessons and full-length mocks.
See placement courses ↗