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.
Consider the code segment written below in C++ :
if (count < 10)//if#1
if ((count%4) ==2)//if#2
cout<<" condition;white/n";
else//(Indentation is wrong)
cout<<" condition is:tan/n";
There are 2 if statements and one else.
To which if, the else statement belong?
AIt belongs to if # 1
BIt belongs to if # 2
CIt belongs to both the if statements.
DIt is independent.
Show answerHide answer
Correct answer: (B) It belongs to if # 2
Explanation
In C++ an else pairs with the nearest preceding unmatched if (the dangling-else rule), whatever the indentation. So the else belongs to if#2.
Consider the following Inheritance in C++ :
class Person
{
char AadharNo[12];
protected :
char Name[20];
public :
______
______
void Readdata( );
};
class Student : public Person
{
int Grade;
public :
______
______
};
What will be the visibility mode of Name and Readdata ( ) in the derived class student ?
AName → Protected, Readdata → Protected
BName → Protected, Readdata → Public
CName → Private, Readdata → Protected
DName → Public, Readdata → Public
Show answerHide answer
Correct answer: (B) Name → Protected, Readdata → Public
Explanation
With public inheritance, protected members stay protected and public members stay public in the derived class. So Name is protected and Readdata() is public. The private AadharNo is not accessible in Student.
How many characters does an escape sequence (\On, \Hn, \n, \f) in C++ consume?
A1
B3
C2
DNone of these
Show answerHide answer
Correct answer: (A) 1
Explanation
An escape sequence is written with two or more characters in the source code, but it represents and stores a single character, so it takes 1 character (1 byte).
Consider the following code segment in C++. How many times the for loop will be repeated?
int main ( )
{
int f = 1;
for (; f ;)
cout <<"f = " <<f++<<"\n";
return 0;
}
A10 times
BNot even once
CRepeated forever
DOnly once
Show answerHide answer
Correct answer: (C) Repeated forever
Explanation
for(; f ;) has no update expression of its own, and f starts at 1 and is incremented each time by f++. f never becomes 0 in normal counting, so the condition stays true and the loop runs forever.
Which of the following statement(s) is/are correct regarding void keyword in C++?
I. To specify the return type of a function when it is not returning any value.
II. To indicate an empty argument list to a function.
AOnly I
BOnly II
CBoth I and II
DNeither I nor II
Show answerHide answer
Correct answer: (C) Both I and II
Explanation
void is used as the return type of a function that returns nothing, and as in f(void) it shows an empty argument list.
Which statement is correct about static member function in C++?
I. It can access to only other static members declared in the same class.
II. It cannot be called using the class name (instead of its objects)
AOnly I
BOnly II
CBoth I and II
DNeither I nor II
Show answerHide answer
Correct answer: (A) Only I
Explanation
A static member function can access only static members of its class, and it can be called with the class name (Class::f()). So only I is correct.
Match the following file stream classes in C++ with its functions.
A. Fstream base — i. Provides support for simultaneous input and output operation
B. If stream — ii. Provides output operation
C. Of stream — iii. Provides operations common to file streams
D. F stream — iv. Provides input operation
AA-i, B-ii, C-iv, D-iii
BA-iv, B-iii, C-i, D-ii
CA-i, B-iii, C-iv, D-ii
DA-iii, B-iv, C-ii, D-i
Show answerHide answer
Correct answer: (D) A-iii, B-iv, C-ii, D-i
Explanation
fstreambase provides operations common to file streams, ifstream gives input, ofstream gives output, and fstream supports simultaneous input and output.
Which of the following statement is NOT correct regarding Virtual function in C++?
I. It must be a member of some class.
II. It is accessed by using object points.
AOnly I
BOnly II
CBoth I and II
DNeither I nor II
Show answerHide answer
Correct answer: (D) Neither I nor II
Explanation
Both statements are true: a virtual function must be a member of a class, and it is accessed through object pointers (base-class pointers) for run-time polymorphism. So neither is incorrect.
Q20DSSSB TGT Computer Science (Technical), 8 Aug 2021, Shift 2
What is the output of the code written in C?
void main()
{
int k = 8, v = 8, i;
i = k/++v;
printf("%d/n", i);
}
A0
B1
C0.8
DError
Show answerHide answer
Correct answer: (A) 0
Explanation
++v makes v = 9 before the division. Then i = 8/9 = 0 because integer division drops the fraction. printf prints 0 (followed by the literal text "/n").
Q21DSSSB TGT Computer Science (Technical), 8 Aug 2021, Shift 2
Consider the following C declaration. Assume that objects of data type short, float and long occupy 2 bytes, 4 bytes and 8 bytes respectively. What is the memory requirement for variable t?
struct {
short a[5];
union {
float v;
long z;
} u;
} t;
A22 bytes
B18 bytes
C24 bytes
D10 bytes
Show answerHide answer
Correct answer: (B) 18 bytes
Explanation
short a[5] takes 5 × 2 = 10 bytes. The union takes the size of its largest member, long, which is 8 bytes. Total = 10 + 8 = 18 bytes, ignoring alignment padding.
Q22DSSSB TGT Computer Science (Technical), 7 Aug 2021, Shift 3
What is the output of the code written in C?
void main()
{
int k, v;
k = (v=20, v+2);
printf("%d/n", k);
}
A2
B20
C22
DError
Show answerHide answer
Correct answer: (C) 22
Explanation
The comma operator evaluates v = 20, then v + 2, and returns the last value, so k = 22. (The "/n" is printed literally after it.)
Q23DSSSB TGT Computer Science (Technical), 7 Aug 2021, Shift 2
Which one is the reserved word in C language?
AAutomatic
BInteger
CConstant
DVolatile
Show answerHide answer
Correct answer: (D) Volatile
Explanation
volatile is a C keyword (type qualifier). Automatic, Integer and Constant are not keywords; the C keywords are auto, int and const.
Q24DSSSB TGT Computer Science (Technical), 7 Aug 2021, Shift 2
What is the output of the code written in C?
void main()
{
int k, val = 1;
k = ++val * ++val;
printf("%d", k);
}
A1
B4
C9
D16
Show answerHide answer
Correct answer: (C) 9
Explanation
Modifying val twice in one expression is undefined behaviour in C, but common compilers (e.g. GCC) apply both increments first, making val = 3, and then compute 3 * 3 = 9.
Q25DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 3
Which statements is correct regarding main() function in C++?
I. It is a user defined function.
II. It is a pre-defined function.
AOnly I
BOnly II
CBoth I and II
DNeither I nor II
Show answerHide answer
Correct answer: (A) Only I
Explanation
main() is called by the system, but its body is written by the programmer, so it is treated as a user-defined function, not a library (pre-defined) function.
Q26DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 3
Which inbuilt function in C is used to read string from system console?
APuts()
BCgets()
CGetche()
DGetchar()
Show answerHide answer
Correct answer: (B) Cgets()
Explanation
cgets() (from conio.h) reads a string from the console. puts() writes a string, and getche()/getchar() read single characters.
Q27DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 3
What is the output of the following code written in C?
void main()
{
printf("%d%d%d", 62, 062, 0x62);
}
A506298
B629850
C625098
D986250
Show answerHide answer
Correct answer: (C) 625098
Explanation
62 is decimal, 062 is octal = 6×8 + 2 = 50, and 0x62 is hexadecimal = 6×16 + 2 = 98. With no separators, the output is 625098.
Q28DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 1
Which statement(s) is/are true about formation of identifier in C?
I. It consist of alphabets, digits and underscore.
II. An identifier can start with an underscore.
AOnly I
BOnly II
CBoth I and II
DNeither I nor II
Show answerHide answer
Correct answer: (C) Both I and II
Explanation
A C identifier may contain letters, digits and underscores, and may begin with a letter or an underscore (but not a digit). Both statements are true.
Q29DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 1
In C++ a wide-character constant begins with the letter ________.
AW
BC
CL
DS
Show answerHide answer
Correct answer: (C) L
Explanation
A wide-character literal is written with the prefix L, for example L'a', and has type wchar_t.
Q30DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 1
What is the output of the code written in C?
void main()
{
printf("%d", 7%-3);
printf("%d", -7%3);
}
A1 −1
B−1 −1
C−1 1
D1 1
Show answerHide answer
Correct answer: (A) 1 −1
Explanation
In C the result of % takes the sign of the dividend: 7 % −3 = 1 and −7 % 3 = −1. The two printf calls print 1 and −1.
What will be the output of the following code?
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
printf("%d %d \n", arr[2], *(arr + 2));
return 0;
}
A3 3
B2 2
CCompilation Error
DUndefined Behavior
Show answerHide answer
Correct answer: (A) 3 3
Explanation
arr[2] and *(arr + 2) both mean the third element, 3, so "3 3" is printed.
What is the output of the following code? #include <stdio.h>
void func(int *p) {
int q = 10;
p = &q;
}
int main() {
int x = 20, *ptr = &x;
func(ptr);
printf("%d\n", *ptr);
return 0;
}
ACompile time error
B20
C10
DGarbage value
Show answerHide answer
Correct answer: (B) 20
Explanation
C passes the pointer by value, so p = &q changes only the local copy in func. ptr still points to x, and 20 is printed.
What will be the output of the following code?
#include<stdio.h>
int main() {
int x=5,y;
y=x++ + ++x;
printf("%d",y);
return 0;
}
A11
Bcompile time error
C12
D10
Show answerHide answer
Correct answer: (C) 12
Explanation
x++ gives 5 (x becomes 6), then ++x makes x 7, so y = 5 + 7 = 12 on common compilers. Strictly, modifying x twice in one expression is undefined behaviour in C.
What will be the output of the following code when the input is HelloWorld?
#include <stdio.h>
int main() {
char str[5]; // Buffer for input
int result = scanf("%4s", str);
if (result == 1) {
printf("Input: %s\n", str);
} else {
printf("Input error or no input.\n");
}
return 0;
}
AInput: Hell
BInput: Hello
CInput error or no input
DInput: HelloWorld
Show answerHide answer
Correct answer: (A) Input: Hell
Explanation
The width %4s makes scanf read at most 4 characters, so str gets "Hell" plus '\0' (fits in 5 bytes). scanf returns 1, so "Input: Hell" is printed.
What will be the output of the following code?
#include <stdio.h>
int main() {
if (5 > 10)
printf("5 is greater than 10");
else
printf("5 is not greater than 10");
return 0;
}
ANo output
BSyntax error
C5 is greater than 10
D5 is not greater than 10
Show answerHide answer
Correct answer: (D) 5 is not greater than 10
Explanation
5 > 10 is false, so the else branch runs and prints "5 is not greater than 10".
#include <stdio.h>
int main() {
FILE *file = fopen("input.txt", "r");
int value;
fscanf(file, "%d", &value);
fseek(file, 0, SEEK_SET);
fscanf(file, "%d", &value);
fclose(file);
printf("%d\n", value);
return 0;
}
What will be the output of the code if input.txt contains 42?
AUndefined behavior
B42
CEOF
DIt depends on the system
Show answerHide answer
Correct answer: (B) 42
Explanation
The first fscanf reads 42. fseek(file, 0, SEEK_SET) moves back to the start, so the second fscanf reads 42 again, and 42 is printed.
What is the output of the following code?
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
A*
**
***
B***
**
*
C***
***
***
DCompilation Error
Show answerHide answer
Correct answer: (A) *
**
***
Explanation
The outer loop runs i = 1 to 3, and the inner loop prints i stars each time, so the rows are *, ** and ***.
What is the output of the following code?
#include <stdio.h>
void func(void);
int main() {
func(10);
return 0;
}
void func(void)
{
printf("Function called\n");
}
ACompilation error
BCompiles successfully but no output displayed
CPrints 'Function called'
DPrints 'Function not called'
Show answerHide answer
Correct answer: (A) Compilation error
Explanation
func is declared as void func(void), so it takes no arguments. Calling func(10) passes an argument, which the compiler rejects as too many arguments.
In C, which of the following file opening modes allow both reading and appending data in a
file?
A"w+"
B"a"
C"r"
D"a+"
Show answerHide answer
Correct answer: (D) "a+"
Explanation
Mode "a+" opens a file for reading and appending: you can read anywhere, and every write goes to the end. "a" only appends, and "w+" truncates the file first.
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.