Programming in C/C++ Previous Year Questions (page 2)
89 Programming in C/C++ (Computer Science) questions asked in DSSSB TGT / PGT previous year papers, each with the correct answer and explanation. Read them free on UTpaper, or attempt the full papers as timed online tests.
What will be the output of the following C program?
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}
AInfinite Loop
B1 2 3 4 5 6
C1 2 3 4 5
D2 3 4 5 6
Show answerHide answer
Correct answer: (C) 1 2 3 4 5
Explanation
The do-while prints i and then increments it while i <= 5, so it prints 1 2 3 4 5 and stops when i becomes 6.
Which of the following statements about do-while loop in C program is/are correct?
P: In do-while loop, first the condition is evaluated and then the statements are executed.
Q: In do-while loop, firstly the statements inside loop body are executed and then the
condition is evaluated.
R: The body of the do-while loop cannot contain a block of statements.
AOnly Q and R
BOnly Q
CP, Q and R
DOnly P
Show answerHide answer
Correct answer: (B) Only Q
Explanation
A do-while loop runs its body first and then tests the condition, so Q is true and P is false. R is false because the body can be a block in braces.
Correct answer: (B) The variable contains garbage (indeterminate) value.
Explanation
A local (automatic) variable that is not initialised holds an indeterminate (garbage) value. The program still compiles and runs, but results are unpredictable.
What is the output of the following code?
#include <stdio.h>
int main() {
printf("%d", printf("C Programming"));
return 0;
}
A13
BC Programming
CC Programming12
DC Programming13
Show answerHide answer
Correct answer: (D) C Programming13
Explanation
The inner printf runs first, prints "C Programming" and returns the number of characters printed, 13. The outer printf then prints 13, giving "C Programming13".
Consider the following code snippet. What will it print?
#include <stdio.h>
int main() {
char str[][10] = {"one", "two", "three"};
printf("%c\n", str[2][4]);
return 0;
}
Ar
Be
Cthree
Dh
Show answerHide answer
Correct answer: (B) e
Explanation
str[2] is "three" and indexing from 0 gives t(0), h(1), r(2), e(3), e(4), so str[2][4] is 'e'.
What will be the output of the following code?
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int (*p)[5] = &arr;
printf("%d\n", *(*p + 3));
return 0;
}
A40
B30
C20
DCompilation Error
Show answerHide answer
Correct answer: (A) 40
Explanation
p points to the whole array, so *p is arr (the address of its first element). *p + 3 points to arr[3], and dereferencing gives 40.
What will be printed by the following code?
#include<stdio.h>
int main() {
int marks = 75;
char grade = (marks >= 90) ? 'A' : (marks >= 70) ? 'B' : 'C';
printf("%c", grade);
return 0;
}
AB
BCompile time error
CA
DC
Show answerHide answer
Correct answer: (A) B
Explanation
75 >= 90 is false, so the second condition is checked: 75 >= 70 is true, so grade is 'B'.
What will be the output of the following code (assume size of int as 4 bytes in the
system)?
#include<stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("%lu", sizeof(arr));
return 0;
}
A20
B5
Ccompile time error
D4
Show answerHide answer
Correct answer: (A) 20
Explanation
sizeof on an array gives its total size in bytes: 5 elements x 4 bytes = 20.
fclose(fp) closes a file opened with fopen and flushes its buffers. close() is the low-level POSIX call on file descriptors, not the standard C file function.
What will be the output of the following C program code if the file "test.txt" is empty,
readable, and exists in the appropriate directory?
#include <stdio.h>
int main() {
FILE *fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("Error\n");
} else {
printf("Success\n");
fclose(fp);
}
return 0;
}
ASuccess
BNULL
CGarbage value
DError
Show answerHide answer
Correct answer: (A) Success
Explanation
fopen in "r" mode succeeds for an existing readable file even if it is empty, so fp is not NULL and "Success" is printed. Emptiness only matters when reading data.
What will be the output of the following C code?
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++)
{
if (++i % 2 == 0)
printf("%d ", i);
}
return 0;
}
A1 2 3 4 5 6 7 8 9 10
B2 4 6 8 10
C1 3 5 7 9
D2 4 6 8
Show answerHide answer
Correct answer: (B) 2 4 6 8 10
Explanation
++i inside the if makes i even each time it is tested: i goes 1 to 2 (printed), then the loop's i++ makes 3, ++i makes 4 (printed), and so on up to 10. Then i becomes 11 and the loop stops, so 2 4 6 8 10 is printed.
What is the output of the given C programming code?
#include <stdio.h>
void modify(int arr[], int n)
{
for(int i = 0; i < n; i++)
arr[i] = arr[i] * 2;
}
int main()
{
int data[] = {1, 2, 3, 4};
modify(data, 4);
printf("%d", data[2]);
return 0;
}
A8
B4
C6
D2
Show answerHide answer
Correct answer: (C) 6
Explanation
Arrays are passed as a pointer to their first element, so modify() doubles the original elements: data becomes {2, 4, 6, 8}, and data[2] = 6.
Which of the following statements is correct about function prototypes in C?
AFunction prototype provides the code of the function, which will be executed when
called.
BFunction prototype provides the complete implementation of a function, including the
return type, name, parameters, and statements that make up the function’s body.
CFunction prototypes tell the compiler the number and type of arguments that are to be
passed to the function and the type of value that is to be returned by the function.
DFunction prototypes are typically placed at the end of the code after the main function.
Show answerHide answer
Correct answer: (C) Function prototypes tell the compiler the number and type of arguments that are to be
passed to the function and the type of value that is to be returned by the function.
Explanation
A prototype declares the function's return type, name and the number and types of its parameters (ending with a semicolon), so the compiler can check calls. It has no body and is usually placed before main.
What happens when the following code executes?
#include <stdio.h>
int main() {
char buffer[50];
int n = sprintf(buffer, "Hello, World!");
printf("%d %s", n, buffer);
return 0;
}
APrints '13 Hello'
BPrints '12 Hello'
CPrints '12 Hello, World!'
DPrints '13 Hello, World!'
Show answerHide answer
Correct answer: (D) Prints '13 Hello, World!'
Explanation
sprintf writes "Hello, World!" into buffer and returns the number of characters written, which is 13 (5 + comma + space + 5 + !). printf then prints "13 Hello, World!".
What will be the output of the following code?
#include <stdio.h>
int main() {
int x = -1, y = 0, z = 1;
int result = (x > 0) ? x : (y == 0) ? z : y;
printf("%d", result);
return 0;
}
Acompile time error
B−1
C1
D0
Show answerHide answer
Correct answer: (C) 1
Explanation
x > 0 is false, so the second conditional (y == 0) ? z : y is evaluated. y is 0, so the result is z = 1.
How many times will the innermost loop execute in the below code?
#include <stdio.h>
int main() {
for(int i=0; i<3; i++)
{
for(int j=0; j<2; j++)
{
printf("%d ",i);
}
}
return 0;
}
A6 times
B3 times
C5 times
D2 times
Show answerHide answer
Correct answer: (A) 6 times
Explanation
The outer loop runs 3 times (i = 0,1,2) and the inner loop runs 2 times for each, so the inner body executes 3 x 2 = 6 times.
What will be the output of the following code snippet?
#include<stdio.h>
int main() {
int a = 5, b = 3, c = 0;
if (a > b > c) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}
Acompile time error
Bundefined behavior
CTRUE
DFALSE
Show answerHide answer
Correct answer: (C) TRUE
Explanation
Relational operators are evaluated left to right: (5 > 3) gives 1, then 1 > 0 gives 1 (true), so "True" is printed.
What will be the output of the following code?
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("%d", max);
return 0;
}
A30
Bcompile time error
C10
D20
Show answerHide answer
Correct answer: (D) 20
Explanation
a > b (10 > 20) is false, so the conditional operator returns b, and 20 is printed.
Which of the following statements is INCORRECT about the storage classes in C?
AThe scope of variable in automatic storage class is local to the block in which the
variable is defined.
BIn Register storage class, the default initial value of the variable is zero.
CIn static storage class, the default initial value of the variable is zero.
DIn Register storage class, the storage of a variable is CPU register.
Show answerHide answer
Correct answer: (B) In Register storage class, the default initial value of the variable is zero.
Explanation
Register variables, like automatic ones, have a garbage (undefined) initial value if not initialised; only static and global variables default to zero. So statement B is incorrect.
What will be the output of the given C program?
#include <stdio.h>
int main()
{
{
int x = 10, y = 20;
{
{
int y = 40;
x++;
y++;
}
printf("x = %d, y = %d\n", x, y);
}
}
return 0;
}
Ax = 11, y = 41
Bx = 10, y = 41
Cx = 10, y = 20
Dx = 11, y = 20
Show answerHide answer
Correct answer: (D) x = 11, y = 20
Explanation
The inner block declares a new y = 40, which shadows the outer y; y++ changes only that inner copy. x is not redeclared, so x++ changes the outer x to 11. The printf sees x = 11 and the outer y = 20.
Which of the following options is correct about the functions in C language?
AActual parameters are declared in the function definition.
BActual parameters are always variables.
CActual parameters are used to define the function signature and to specify the type and
number of arguments that a function can accept.
DActual parameters are the values that are passed to a function when it is called.
Show answerHide answer
Correct answer: (D) Actual parameters are the values that are passed to a function when it is called.
Explanation
Actual parameters (arguments) are the values or expressions passed in the function call. The parameters declared in the function definition are formal parameters, and actual arguments need not be variables (constants and expressions work too).
What will be the output of the given C program?
#include <stdio.h>
int main() {
int a = 4;
int b = 3;
int result;
result = a ^ b;
printf("%d", result);
return 0;
}
A0
B3
C4
D7
Show answerHide answer
Correct answer: (D) 7
Explanation
^ is bitwise XOR: 4 = 100 and 3 = 011, so 100 XOR 011 = 111 = 7.
Which of the following is true about the for loop in C?
AA for loop cannot be nested inside another for loop.
BThe condition in a for loop is optional, and it can result in an infinite loop if omitted.
CA for loop must always have all three components: initialization, condition, and
increment/decrement.
DThe increment/decrement part of the for loop is mandatory.
Show answerHide answer
Correct answer: (B) The condition in a for loop is optional, and it can result in an infinite loop if omitted.
Explanation
All three parts of a for loop are optional. If the condition is omitted, it is treated as always true, e.g. for(;;), giving an infinite loop unless break is used. For loops can also be nested.
Which of the following will terminate the loop immediately in C?
AWhen a break statement is encountered
BWhen the loop variable reaches a certain value
CWhen a continue statement is encountered
DWhen the loop condition is not given
Show answerHide answer
Correct answer: (A) When a break statement is encountered
Explanation
break exits the enclosing loop immediately, regardless of the condition. continue only skips the rest of the current iteration and goes on with the next one.
What will be the output of the following C program?
#include <stdio.h>
int main()
{
int a[5];
for (int i = 0; i < 5; i++)
{
a[i] = i * i - 2 * i + 1;
}
for (int i = 0; i < 5; i++)
{
printf("%d ", a[i]);
}
return 0;
}
A1 0 2 4 9
B1 0 1 4 9
C1 1 2 4 9
D1 2 3 4 9
Show answerHide answer
Correct answer: (B) 1 0 1 4 9
Explanation
i*i - 2*i + 1 = (i - 1)², so for i = 0 to 4 the values are 1, 0, 1, 4, 9.
What will the output of the given program be?
#include<stdio.h>
main()
{
printf(6+"Hello World\n");
}
A6He110 world
BCompilation error
CWorld
DHello
Show answerHide answer
Correct answer: (B) Compilation error
Explanation
Note: the answer key gives (B), but the code compiles. 6 + "Hello World\n" is pointer arithmetic that skips the first 6 characters, so printf prints 'World' (C).
In C, variable list arguments are denoted by the standard header:
A<unistd.h>
B<string.h>
C<stdio.h >
D<stdarg.h>
Show answerHide answer
Correct answer: (C) <stdio.h >
Explanation
Note: the answer key gives (C), but variable argument lists in C (va_list, va_start, va_arg, va_end) are defined in the standard header <stdarg.h> (D).
scanf() is a predefined function in which of the following header files?
Actype.h
Bstdio.h
Cunistd.h
Dstring.h
Show answerHide answer
Correct answer: (B) stdio.h
Explanation
scanf() and printf() are standard input/output functions declared in stdio.h.
How to use these DSSSB TGT / PGT Computer Science questions
Every question here was asked in a real DSSSB TGT / PGT paper; the paper and shift are shown on the card, and the number opens the question on its own page. Try each one before tapping Show answer, then attempt the full DSSSB TGT / PGT papers as timed tests to check your speed under exam conditions.