DSSSB TGT / PGT · Computer Science

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.

  • 89 questions
  • Official answer key
  • Free

Other Computer Science topics

Q51DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 3 (5:00 PM)
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; }
  1. AInfinite Loop
  2. B1 2 3 4 5 6
  3. C1 2 3 4 5
  4. 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.

Q52DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 3 (5:00 PM)
Consider the expression given below in C. x ? y : a ? b : c What will be the value of the given expression if x = 0 and a = 0?
  1. AEquivalent to the value of y.
  2. BEquivalent to the value of c.
  3. C0
  4. DEquivalent to the value of b.
Show answerHide answer

Correct answer: (B) Equivalent to the value of c.

Explanation

The ?: operator is right-associative, so the expression is x ? y : (a ? b : c). x = 0 picks the second part, and a = 0 then picks c.

Q53DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 3 (5:00 PM)
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.
  1. AOnly Q and R
  2. BOnly Q
  3. CP, Q and R
  4. 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.

Q54DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
What happens if you try to use an uninitialised variable in C?
  1. AThe program will throw a runtime error.
  2. BThe variable contains garbage (indeterminate) value.
  3. CThe program will not compile.
  4. DThe variable is automatically initialised to 0.
Show answerHide answer

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.

Q55DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
What is the output of the following code? #include <stdio.h> int main() { printf("%d", printf("C Programming")); return 0; }
  1. A13
  2. BC Programming
  3. CC Programming12
  4. 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".

Q56DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
How many times will the following loop execute? for (int i = 0; i < 5; i++) { printf("%d ", i); }
  1. AInfinite
  2. B4
  3. C5
  4. D6
Show answerHide answer

Correct answer: (C) 5

Explanation

i takes values 0, 1, 2, 3, 4; when i becomes 5 the condition i < 5 fails, so the loop executes 5 times.

Q57DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. Ar
  2. Be
  3. Cthree
  4. 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'.

Q58DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
Which of the following is an invalid variable name in C?
  1. AfirstName
  2. Bvoid
  3. Ctotal_sum
  4. Dnum123
Show answerHide answer

Correct answer: (B) void

Explanation

void is a reserved keyword in C and cannot be used as an identifier. The other names use only letters, digits and underscores and start with a letter.

Q59DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. A40
  2. B30
  3. C20
  4. 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.

Q60DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. AB
  2. BCompile time error
  3. CA
  4. 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'.

Q61DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. A20
  2. B5
  3. Ccompile time error
  4. 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.

Q62DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
What is the primary restriction on nested functions in C?
  1. ANested functions are not allowed in standard C but are supported as extensions in some compilers.
  2. BNested functions must be declared as inline.
  3. CNested functions must be defined before their enclosing function.
  4. DNested functions are fully supported in standard C.
Show answerHide answer

Correct answer: (A) Nested functions are not allowed in standard C but are supported as extensions in some compilers.

Explanation

Standard C does not allow a function to be defined inside another function. GCC supports nested functions only as a compiler extension.

Q63DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
Which function is used to close a file in C?
  1. Aclose()
  2. Bfclose()
  3. Cflush()
  4. Dendfile()
Show answerHide answer

Correct answer: (B) fclose()

Explanation

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.

Q64DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. ASuccess
  2. BNULL
  3. CGarbage value
  4. 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.

Q65DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. A1 2 3 4 5 6 7 8 9 10
  2. B2 4 6 8 10
  3. C1 3 5 7 9
  4. 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.

Q66DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
What will be the output of the following C code? #include <stdio.h> int main() { int i = 1; while (i < 5) { printf("%d ", i++); i++; } return 0; }
  1. A2 4
  2. B1 3 4
  3. C1 3
  4. D1 2 3 4 5
Show answerHide answer

Correct answer: (C) 1 3

Explanation

i = 1 is printed and becomes 2, then i++ makes it 3; 3 is printed and becomes 4, then 5. Now 5 < 5 is false, so the output is 1 3.

Q67DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
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; }
  1. A8
  2. B4
  3. C6
  4. 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.

Q68DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)
Which of the following statements is correct about function prototypes in C?
  1. AFunction prototype provides the code of the function, which will be executed when called.
  2. BFunction prototype provides the complete implementation of a function, including the return type, name, parameters, and statements that make up the function’s body.
  3. 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.
  4. 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.

Q69DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. APrints '13 Hello'
  2. BPrints '12 Hello'
  3. CPrints '12 Hello, World!'
  4. 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!".

Q70DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
What is the output of the following code? #include <stdio.h> int main() { int i = 5; do { printf("%d ", i); i--; } while (i > 5); return 0; }
  1. ANo output printed
  2. BInfinite loop
  3. C5 4 3 2 1
  4. D5
Show answerHide answer

Correct answer: (D) 5

Explanation

A do-while body runs at least once, so 5 is printed. Then i becomes 4 and the condition 4 > 5 is false, so the loop ends.

Q71DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. Acompile time error
  2. B−1
  3. C1
  4. 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.

Q72DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. A6 times
  2. B3 times
  3. C5 times
  4. 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.

Q73DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
Which arithmetic operator has the highest precedence in C?
  1. A−
  2. B=
  3. C+
  4. D%
Show answerHide answer

Correct answer: (D) %

Explanation

Among the given operators, % shares the highest (multiplicative) precedence level with * and /, above + and - (additive) and = (assignment, lowest).

Q74DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. Acompile time error
  2. Bundefined behavior
  3. CTRUE
  4. 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.

Q75DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. A30
  2. Bcompile time error
  3. C10
  4. 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.

Q76DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
Which of the following statements is INCORRECT about the storage classes in C?
  1. AThe scope of variable in automatic storage class is local to the block in which the variable is defined.
  2. BIn Register storage class, the default initial value of the variable is zero.
  3. CIn static storage class, the default initial value of the variable is zero.
  4. 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.

Q77DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. Ax = 11, y = 41
  2. Bx = 10, y = 41
  3. Cx = 10, y = 20
  4. 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.

Q78DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
Which of the following options is correct about the functions in C language?
  1. AActual parameters are declared in the function definition.
  2. BActual parameters are always variables.
  3. CActual parameters are used to define the function signature and to specify the type and number of arguments that a function can accept.
  4. 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).

Q79DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. A0
  2. B3
  3. C4
  4. D7
Show answerHide answer

Correct answer: (D) 7

Explanation

^ is bitwise XOR: 4 = 100 and 3 = 011, so 100 XOR 011 = 111 = 7.

Q80DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
Which of the following is true about the for loop in C?
  1. AA for loop cannot be nested inside another for loop.
  2. BThe condition in a for loop is optional, and it can result in an infinite loop if omitted.
  3. CA for loop must always have all three components: initialization, condition, and increment/decrement.
  4. 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.

Q81DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
Which of the following will terminate the loop immediately in C?
  1. AWhen a break statement is encountered
  2. BWhen the loop variable reaches a certain value
  3. CWhen a continue statement is encountered
  4. 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.

Q82DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)
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; }
  1. A1 0 2 4 9
  2. B1 0 1 4 9
  3. C1 1 2 4 9
  4. 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.

Q83DSSSB TGT Computer Science, 25 Jun 2023, Shift 1 (8:30 AM)
What is the output of following C program? #include<stdio.h> int main() { int x = 8, *y, z; y = &x; printf("%d\n", x * *y * x + *y); return 0; }
  1. A8
  2. BRuntime error
  3. C520
  4. DCompilation error
Show answerHide answer

Correct answer: (C) 520

Explanation

y points to x, so *y = 8. x * *y * x + *y = 8 × 8 × 8 + 8 = 512 + 8 = 520.

Q84DSSSB TGT Computer Science, 25 Jun 2023, Shift 1 (8:30 AM)
Which of the following is FALSE with reference to constructor in C#.NET?
  1. AIt can have a return type.
  2. BAll classes have constructors by default.
  3. CConstructor name must match the class name.
  4. DThe constructor is called when the object is created.
Show answerHide answer

Correct answer: (A) It can have a return type.

Explanation

A constructor in C# has no return type, not even void, so A is false. It has the class name and is called when an object is created.

Q85DSSSB TGT Computer Science, 25 Jun 2023, Shift 1 (8:30 AM)
Which of the following is NOT a storage class in the C programming language?
  1. ARegister
  2. BExtern
  3. CAuto
  4. DIntern
Show answerHide answer

Correct answer: (D) Intern

Explanation

C's storage classes are auto, register, static and extern. 'Intern' is not a storage class.

Q86DSSSB TGT Computer Science, 24 Jun 2023, Shift 3 (4:30 PM)
In C++ language, the instruction 'ignore(x,y);'
  1. Askips the last x characters and stops if character y is encountered
  2. Bskips the first y characters and stops if character x is encountered
  3. Cskips the first x characters and stops if character y is encountered
  4. Dskips the last y characters and stops if character x is encountered
Show answerHide answer

Correct answer: (C) skips the first x characters and stops if character y is encountered

Explanation

cin.ignore(x, y) discards up to x characters from the input, or stops earlier once the delimiter character y has been read and discarded.

Q87DSSSB TGT Computer Science, 25 Jun 2023, Shift 2 (12:30 PM)
What will the output of the given program be? #include<stdio.h> main() { printf(6+"Hello World\n"); }
  1. A6He110 world
  2. BCompilation error
  3. CWorld
  4. 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).

Q88DSSSB TGT Computer Science, 25 Jun 2023, Shift 2 (12:30 PM)
In C, variable list arguments are denoted by the standard header:
  1. A<unistd.h>
  2. B<string.h>
  3. C<stdio.h >
  4. 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).

Q89DSSSB TGT Computer Science, 25 Jun 2023, Shift 2 (12:30 PM)
scanf() is a predefined function in which of the following header files?
  1. Actype.h
  2. Bstdio.h
  3. Cunistd.h
  4. 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.

More DSSSB TGT / PGT subjects