A. Bjarne Stroustrup
B. Dennis Ritchie
C. James Gosling
D. Guido van Rossum
Answer: B. Dennis Ritchie
Solution: C was developed by Dennis Ritchie at Bell Laboratories in the early 1970s.
A. .cpp
B. .java
C. .c
D. .py
Answer: C. .c
Solution: C source files conventionally use the .c extension.
printf() and scanf()?A. stdlib.h
B. stdio.h
C. string.h
D. math.h
Answer: B. stdio.h
Solution: The standard input/output header declares functions such as printf() and scanf().
A. start()
B. main()
C. printf()
D. run()
Answer: B. main()
Solution: A hosted C program begins execution through its main() function.
A. 2value
B. total-marks
C. total_marks
D. float
Answer: C. total_marks
Solution: An identifier cannot begin with a digit, contain a hyphen as part of its name, or be a reserved keyword.
A. display
B. number
C. return
D. main
Answer: C. return
Solution: return is a reserved C keyword. main is a function name, not a keyword.
int?A. %c
B. %d
C. %s
D. %f
Answer: B. %d
Solution: %d is used with printf() to print an int in signed decimal form.
#include <stdio.h>
int main(void) {
printf("%d", 10 + 5);
return 0;
}
A. 10
B. 5
C. 15
D. 105
Answer: C. 15
Solution: The addition operator evaluates 10 + 5 to 15.
A. ==
B. =
C. !=
D. <=
Answer: B. =
Solution: The assignment operator stores the right-hand value in the left-hand modifiable object.
17 % 5?A. 2
B. 3
C. 3.4
D. 5
Answer: A. 2
Solution: The remainder after dividing 17 by 5 is 2.
A. int
B. char
C. float
D. double
Answer: B. char
Solution: A char object stores one C byte, which can represent a character from the execution character set or a small integer value.
A. :
B. ,
C. ;
D. .
Answer: C. ;
Solution: Most simple C statements end with a semicolon.
A. <!-- comment -->
B. // comment
C. ** comment **
D. ## comment
Answer: B. // comment
Solution: C supports // single-line comments and /* ... */ block comments.
A. =
B. ==
C. !=
D. &&
Answer: B. ==
Solution: == compares two operands. A single = performs assignment.
5 > 3 in a C expression?A. 0
B. 1
C. 5
D. 3
Answer: B. 1
Solution: A true relational expression evaluates to 1 in C; a false one evaluates to 0.
A. if
B. include
C. define
D. typedef
Answer: A. if
Solution: The if statement executes a statement or block when its controlling expression is nonzero.
A. for
B. while
C. do-while
D. None of these
Answer: C. do-while
Solution: A do-while loop tests its condition after executing the loop body.
int x = 5;
printf("%d", x++);
A. 4
B. 5
C. 6
D. Compilation error
Answer: B. 5
Solution: Post-increment uses the original value in the expression, then increments x to 6.
int x = 5;
printf("%d", ++x);
A. 4
B. 5
C. 6
D. 0
Answer: C. 6
Solution: Pre-increment increases x before its value is used.
A. continue
B. break
C. return
D. goto
Answer: B. break
Solution: break terminates the nearest enclosing loop or switch statement.
A. break
B. exit
C. continue
D. switch
Answer: C. continue
Solution: continue proceeds to the next iteration according to the loop's control rules.
A. -1
B. 0
C. 1
D. Depends on the compiler
Answer: B. 0
Solution: C arrays use zero-based indexing.
int a[10];
A. 9
B. 10
C. 11
D. 40 on every platform
Answer: B. 10
Solution: The array contains 10 elements. Its total size in bytes depends on sizeof(int).
A. *
B. &
C. %
D. ->
Answer: B. &
Solution: The address-of operator & obtains an object's address when applied to an appropriate operand.
A. &
B. #
C. *
D. .
Answer: C. *
Solution: Unary * accesses the object designated by a valid pointer.
A. printf()
B. malloc()
C. strlen()
D. fopen()
Answer: B. malloc()
Solution: malloc() allocates a block of memory and returns a pointer to it, or a null pointer if allocation fails.
A. delete()
B. remove()
C. free()
D. clear()
Answer: C. free()
Solution: free() releases memory obtained from a compatible allocation function such as malloc() or calloc().
A. '\n'
B. '\0'
C. ' '
D. '\t'
Answer: B. '\0'
Solution: A C string is a sequence of characters terminated by a null character.
A. sizeof()
B. strcpy()
C. strlen()
D. strcmp()
Answer: C. strlen()
Solution: strlen() counts characters before the first terminating '\0'.
A. #define
B. #include
C. #ifdef
D. #undef
Answer: B. #include
Solution: #include directs the preprocessor to process the specified header or source file.
int a = 10, b = 3;
printf("%d", a / b);
A. 3.33
B. 3
C. 1
D. 4
Answer: B. 3
Solution: Both operands are integers, so integer division discards the fractional part.
printf("%.1f", 7 / 2.0);
A. 3
B. 3.0
C. 3.5
D. 4.0
Answer: C. 3.5
Solution: Because one operand is a floating-point value, division produces 3.5. %.1f prints one digit after the decimal point.
int x = 4;
x += 3 * 2;
printf("%d", x);
A. 10
B. 14
C. 11
D. 24
Answer: A. 10
Solution: Multiplication occurs first: 3 * 2 = 6. Then x += 6 changes x from 4 to 10.
int x = 8;
printf("%d", x > 5 && x < 10);
A. 0
B. 1
C. 8
D. Compilation error
Answer: B. 1
Solution: Both comparisons are true, so the logical AND expression evaluates to 1.
int x = 0;
printf("%d", !x);
A. 0
B. 1
C. -1
D. x
Answer: B. 1
Solution: Zero represents false in a logical context. The logical NOT operator converts it to 1.
int x = 7;
if (x % 2 == 0)
printf("Even");
else
printf("Odd");
A. Even
B. Odd
C. 7
D. No output
Answer: B. Odd
Solution: 7 % 2 equals 1, so the else branch executes.
int x = 2;
switch (x) {
case 1: printf("A"); break;
case 2: printf("B");
case 3: printf("C"); break;
default: printf("D");
}
A. B
B. BC
C. BCD
D. C
Answer: B. BC
Solution: Execution begins at case 2. Without a break after printing B, control falls through to case 3 and prints C.
for (int i = 1; i <= 5; i++)
printf("*");
A. 4
B. 5
C. 6
D. Infinite times
Answer: B. 5
Solution: The values of i are 1, 2, 3, 4, and 5.
int i = 1;
while (i < 4) {
printf("%d", i);
i++;
}
A. 123
B. 1234
C. 234
D. 111
Answer: A. 123
Solution: The loop executes for i = 1, 2, and 3. It stops when i becomes 4.
int i = 0;
do {
printf("%d", i);
i++;
} while (i < 0);
A. No output
B. 0
C. 01
D. Infinite loop
Answer: B. 0
Solution: The loop body runs once before the condition is checked. The condition is then false.
int a[] = {10, 20, 30, 40};
printf("%d", a[2]);
A. 10
B. 20
C. 30
D. 40
Answer: C. 30
Solution: Array indexing begins at zero, so a[2] is the third element.
A. sizeof(a)
B. sizeof(a[0])
C. sizeof(a) / sizeof(a[0])
D. sizeof(a[0]) / sizeof(a)
Answer: C. sizeof(a) / sizeof(a[0])
Solution: The total array size in bytes divided by the size of one element gives the number of elements. This works when a is an actual array, not a pointer or a function parameter adjusted to a pointer.
int x = 10;
int *p = &x;
*p = 25;
printf("%d", x);
A. 10
B. 25
C. The address of x
D. Compilation error
Answer: B. 25
Solution: p points to x. Assigning 25 through *p changes the value of x.
char s[] = "CAT";
printf("%zu", sizeof(s));
A. 2
B. 3
C. 4
D. Depends on the string length at runtime
Answer: C. 4
Solution: The array contains C, A, T, and the terminating null character.
A. strlen()
B. strcpy()
C. strcmp()
D. strchr()
Answer: B. strcpy()
Solution: strcpy(destination, source) copies the source string, including its terminating null character. The destination must have enough space.
strcmp("abc", "abc") return?A. Always 1
B. Always -1
C. 0
D. The length of the string
Answer: C. 0
Solution: strcmp() returns zero when the two strings are equal. For unequal strings, the sign indicates their ordering.
calloc()?A. It opens a file.
B. It allocates memory initialized to all-bits-zero.
C. It frees memory.
D. It copies a string.
Answer: B. It allocates memory initialized to all-bits-zero.
Solution: calloc(n, size) allocates space for an array of n elements, each of size bytes, and initializes the allocated bytes to zero.
A. fopen()
B. fread()
C. fprintf()
D. fclose()
Answer: A. fopen()
Solution: fopen() opens a file using a specified mode, such as "r", "w", or "a".
"w" generally do?A. Opens only an existing file for reading
B. Opens for writing and truncates an existing file, or creates it
C. Opens only for appending
D. Opens for reading and writing without truncation
Answer: B. Opens for writing and truncates an existing file, or creates it
Solution: "w" opens a text stream for writing. Existing file contents are discarded when the file is successfully opened.
A. scanf()
B. fprintf()
C. fscanf()
D. getchar()
Answer: B. fprintf()
Solution: fprintf() formats output and writes it to the specified stream.
#define PI 3.14 do?A. Declares a floating-point variable named PI
B. Defines a macro named PI
C. Defines a function named PI
D. Allocates memory for PI
Answer: B. Defines a macro named PI
Solution: The preprocessor replaces occurrences of the macro name according to macro-expansion rules. It does not create a variable.
typedef?A. To define a macro
B. To create a type alias
C. To allocate memory
D. To terminate a loop
Answer: B. To create a type alias
Solution: typedef introduces an alternative name for an existing type or a type specified in the declaration.
A. All members must have the same type.
B. Its members can have different types.
C. It cannot contain an array.
D. It cannot contain another structure.
Answer: B. Its members can have different types.
Solution: A struct groups related members, which can have different types, into one aggregate type.
A. Every member always occupies a separate memory region.
B. Its members share the same storage.
C. It cannot contain integers.
D. It is identical to an array.
Answer: B. Its members share the same storage.
Solution: Union members overlap in storage. The size of a union is sufficient for its largest member, with any required padding.
A. A function calling itself directly or indirectly
B. A loop that never stops
C. A function without parameters
D. A variable declared inside a structure
Answer: A. A function calling itself directly or indirectly
Solution: Recursive functions solve problems through repeated function calls and typically require a base case.
static keyword do to a local variable?A. Makes it global in every source file
B. Gives it static storage duration
C. Automatically makes it constant
D. Deletes it after every function call
Answer: B. Gives it static storage duration
Solution: A local static variable retains its stored value between calls and exists for the program's lifetime.
int x = 5;
int y = 2;
printf("%d", x > y ? x : y);
A. 2
B. 5
C. 1
D. 0
Answer: B. 5
Solution: The conditional operator chooses x because x > y is true.
5 & 3?A. 1
B. 2
C. 7
D. 8
Answer: A. 1
Solution: In binary, 5 is 0101 and 3 is 0011. Their bitwise AND is 0001.
5 | 3?A. 1
B. 2
C. 7
D. 15
Answer: C. 7
Solution: The bitwise OR of 0101 and 0011 is 0111, which is 7.
A. pow()
B. sqrt()
C. abs()
D. ceil()
Answer: B. sqrt()
Solution: sqrt() is declared in math.h and calculates the nonnegative square root of a nonnegative real input in its usual domain.
malloc(), calloc(), realloc(), and free()?A. stdio.h
B. string.h
C. stdlib.h
D. ctype.h
Answer: C. stdlib.h
Solution: These dynamic memory management functions are declared in the standard library header stdlib.h.
A. putchar()
B. getchar()
C. puts()
D. fputs()
Answer: B. getchar()
Solution: getchar() reads the next character from standard input and returns it as an int, allowing it to represent EOF.
A. puts()
B. strlen()
C. strcat()
D. fscanf()
Answer: A. puts()
Solution: puts() writes the string and appends a newline.
A. It can point only to an integer.
B. It stores a function's address in a compatible function-pointer type.
C. It is always a null pointer.
D. It is the same as a structure.
Answer: B. It stores a function's address in a compatible function-pointer type.
Solution: Function pointers can be used to call functions indirectly or pass behavior as an argument.
argc conventionally represent in main(int argc, char *argv[])?A. Number of characters in the program name
B. Number of command-line arguments, including the program name
C. Number of functions in the program
D. Number of files opened
Answer: B. Number of command-line arguments, including the program name
Solution: argc counts the strings available through argv, including the program name at argv[0] when supplied by the host environment.
int a = 5, b = 2;
printf("%d", a++ + b);
A. 7
B. 8
C. 9
D. Undefined behavior
Answer: A. 7
Solution: The post-increment expression yields the old value 5. Thus, 5 + 2 = 7, and afterward a becomes 6.
int x = 3;
printf("%d ", x * x + 2);
printf("%d", x);
A. 11 3
B. 25 3
C. 11 5
D. 9 5
Answer: A. 11 3
Solution: Multiplication precedes addition, so the first expression is 9 + 2 = 11. Neither expression changes x.
int i = 1;
printf("%d %d", i++, i++);
A. Always prints 1 2
B. Always prints 2 1
C. Has undefined behavior
D. Does not compile in any conforming C implementation
Answer: C. Has undefined behavior
Solution: The two modifications of i are unsequenced relative to one another. The behavior is undefined; the output cannot be predicted portably.
int a = 0, b = 5;
if (a && b++)
printf("Yes");
printf("%d", b);
A. Yes6
B. 5
C. 6
D. No5
Answer: B. 5
Solution: The left operand of && is false. Short-circuit evaluation skips b++, so b remains 5 and no "Yes" is printed.
int x = 4;
printf("%d", x << 1);
A. 2
B. 4
C. 8
D. 16
Answer: C. 8
Solution: Left-shifting 4 by one bit gives 8. This operation is well-defined here because the positive value is representable in int.
unsigned int?unsigned int x = 0;
x = x - 1;
printf("%u", x);
A. 0
B. -1
C. 4294967295
D. Undefined behavior
Answer: C. 4294967295
Solution: Unsigned arithmetic wraps modulo \(2^{32}\). Subtracting one from zero produces \(2^{32}-1\).
int a[] = {2, 4, 6, 8};
int *p = a;
printf("%d", *(p + 2));
A. 2
B. 4
C. 6
D. 8
Answer: C. 6
Solution: p + 2 points to the third array element, and dereferencing it gives 6.
int a[] = {10, 20, 30};
int *p = a;
printf("%d", *p++);
A. 10
B. 20
C. 30
D. Address of a
Answer: A. 10
Solution: Post-increment has higher precedence than unary dereference in this expression's grouping: *(p++). The current element is read, and then the pointer advances to the next element.
int x = 10;
int *p = &x;
int **q = &p;
printf("%d", **q);
A. The address of x
B. The address of p
C. 10
D. Compilation error
Answer: C. 10
Solution: q points to p, and p points to x. Dereferencing twice retrieves the value of x.
A. int *const p;
B. const int *p;
C. int const *const p;
D. int p const;
Answer: B. const int *p;
Solution: Through p, the pointed-to integer cannot be modified. The pointer itself can be reassigned. Option C makes both the pointed-to integer and the pointer itself const, while A makes only the pointer const.
A. int *const p = &x;
B. const int *p = &x;
C. int **const p = &q;
D. int const p = x;
Answer: A. int *const p = &x;
Solution: p cannot be reassigned after initialization, but the integer it points to can be modified through *p.
int sum(int n) {
if (n == 0)
return 0;
return n + sum(n - 1);
}
What is sum(4)?
A. 4
B. 6
C. 10
D. 24
Answer: C. 10
Solution: The recursive calculation is \(4 + 3 + 2 + 1 + 0 = 10\).
int fact(int n) {
if (n <= 1)
return 1;
return n * fact(n - 1);
}
printf("%d", fact(5));
A. 15
B. 24
C. 120
D. 25
Answer: C. 120
Solution: The function computes \(5! = 5 \times 4 \times 3 \times 2 \times 1 = 120\). This assumes the function definition is visible before the call.
void count(void) {
static int x = 0;
x++;
printf("%d ", x);
}
/* In main: */
count();
count();
count();
A. 1 1 1
B. 0 1 2
C. 1 2 3
D. 3 3 3
Answer: C. 1 2 3
Solution: The static local variable is initialized once and retains its value between calls.
int x = 10;
void change(int x) {
x = 20;
}
/* In main: */
change(x);
printf("%d", x);
A. 10
B. 20
C. 0
D. Undefined behavior
Answer: A. 10
Solution: C passes function arguments by value. The parameter x is a separate local variable, so changing it does not change the caller's variable.
void change(int *p) {
*p = 50;
}
/* In main: */
int x = 10;
change(&x);
printf("%d", x);
A. 10
B. 50
C. The address of x
D. Compilation error
Answer: B. 50
Solution: The function receives a copy of the pointer to x. Dereferencing it allows the function to modify the original object.
sizeof(a) / sizeof(a[0])?char a[] = "HELLO";
A. 4
B. 5
C. 6
D. Depends on the compiler
Answer: C. 6
Solution: The array has five letters plus the terminating null character. The expression counts all six array elements.
char s[] = "HELLO";
s[1] = 'A';
printf("%s", s);
A. HELLO
B. HALLO
C. HALL
D. Undefined behavior
Answer: B. HALLO
Solution: s is a modifiable character array. Replacing the character at index 1 changes E to A.
char *s = "HELLO";
s[0] = 'Y';
A. Nothing is wrong.
B. It always changes the string to YELLO.
C. Modifying a string literal has undefined behavior.
D. The declaration of s is invalid.
Answer: C. Modifying a string literal has undefined behavior.
Solution: A string literal is not a modifiable array for this purpose. The pointer declaration is valid, but writing through it to modify the literal has undefined behavior.
int a = 10;
int *p = &a;
int *q = p;
*q = 20;
printf("%d", a);
A. 10
B. 20
C. The address of a
D. Compilation error
Answer: B. 20
Solution: Both pointers refer to the same integer object. Writing through q changes a.
A. int *p = malloc(10);
B. int *p = malloc(10 * sizeof *p);
C. int p = malloc(10 * sizeof(int));
D. int *p = sizeof(int) * 10;
Answer: B. int *p = malloc(10 * sizeof *p);
Solution: This allocates enough bytes for ten integers and does not hard-code the size of the pointed-to type. In C, no cast is required for the return value of malloc(). The allocation should be checked for failure.
A. Accessing a valid array element
B. Losing access to allocated memory without releasing it
C. Declaring a pointer
D. Closing a file
Answer: B. Losing access to allocated memory without releasing it
Solution: If dynamically allocated memory becomes unreachable before being freed, the program may leak memory.
A. A pointer to a valid object
B. A pointer that refers to an object whose lifetime has ended
C. A pointer to a function
D. A pointer initialized to NULL
Answer: B. A pointer that refers to an object whose lifetime has ended
Solution: Dereferencing a dangling pointer is invalid and can result in undefined behavior.
A. Increment the pointer
B. Set the pointer to NULL
C. Dereference it once
D. Call malloc() on it without assignment
Answer: B. Set the pointer to NULL
Solution: Setting a pointer to NULL helps prevent accidental reuse through that pointer. It does not automatically invalidate other aliases to the same freed allocation.
int x = 5;
printf("%d", (x > 3) ? (x + 2) : (x - 2));
A. 3
B. 5
C. 7
D. 2
Answer: C. 7
Solution: Since x > 3 is true, the conditional operator evaluates x + 2.
#define SQUARE(x) x * x
printf("%d", SQUARE(2 + 3));
A. 25
B. 11
C. 13
D. 10
Answer: B. 11
Solution: Macro expansion produces 2 + 3 * 2 + 3. Multiplication takes precedence, yielding 2 + 6 + 3 = 11. A safer macro is #define SQUARE(x) ((x) * (x)), though arguments with side effects still require care.
A. #define SQUARE(x) x * x
B. #define SQUARE(x) (x * x)
C. #define SQUARE(x) ((x) * (x))
D. #define SQUARE x * x
Answer: C. #define SQUARE(x) ((x) * (x))
Solution: Parentheses around each occurrence of the argument and around the complete expression reduce errors caused by operator precedence. Avoid passing expressions with side effects, because x is evaluated twice.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
printf("%d", MAX(4, 9));
A. 4
B. 9
C. 13
D. Compilation error
Answer: B. 9
Solution: The comparison 4 > 9 is false, so the conditional expression selects 9.
#ifndef in a header guard?A. It terminates a function.
B. It checks whether a macro has not been defined.
C. It allocates memory.
D. It declares a structure.
Answer: B. It checks whether a macro has not been defined.
Solution: Header guards commonly use #ifndef, #define, and #endif to prevent repeated processing of the same header contents within a translation unit.
fseek(fp, 0, SEEK_END) generally do?A. Moves the file position to the beginning
B. Moves the file position to the end
C. Closes the file
D. Deletes the file
Answer: B. Moves the file position to the end
Solution: fseek() changes a stream's file position. With offset zero and SEEK_END, it positions the stream at the end, if the operation is supported and succeeds.
ftell(fp) return on success?A. The file's name
B. The file's size in every possible stream
C. The current file-position indicator as a long value
D. The number of characters printed
Answer: C. The current file-position indicator as a long value
Solution: ftell() returns the current file position in the form defined for the stream, or -1L on failure. Its result is not universally a simple byte count for every stream.
int x = 10;
int y = 20;
int *p = &x;
p = &y;
printf("%d", *p);
A. 10
B. 20
C. The address of y
D. Undefined behavior
Answer: B. 20
Solution: The pointer initially points to x, but then it is reassigned to point to y. Dereferencing it returns 20.
argv[1] conventionally represent when at least one command-line argument is supplied beyond the program name?int main(int argc, char *argv[])
A. The program's return value
B. The first command-line argument after the program name
C. The argument count
D. The last character of the program name
Answer: B. The first command-line argument after the program name
Solution: argv[0] conventionally identifies the program, while argv[1] is the first argument supplied after it. The actual value of argv[0] depends on the host environment.
int a = 5;
int b = 10;
int *p = &a;
int *q = &b;
*p = *q;
printf("%d %d", a, b);
A. 5 10
B. 10 10
C. 5 5
D. 10 5
Answer: B. 10 10
Solution: *p = *q assigns the value of b to a. Therefore, both variables contain 10.
#include <stdio.h>
int main(void) {
int x = 10;
int *p = &x;
int **q = &p;
**q = **q + 5;
printf("%d", x);
return 0;
}
A. 10
B. 15
C. The address of x
D. Undefined behavior
Answer: B. 15
Solution: q points to p, and p points to x. Thus, **q accesses x. Adding 5 changes x from 10 to 15.
A. Dennis Ritchie
B. Bjarne Stroustrup
C. James Gosling
D. Guido van Rossum
Answer: B. Bjarne Stroustrup
Solution: Bjarne Stroustrup developed C++ as an extension of C, initially calling it “C with Classes.”
A. .c
B. .py
C. .cpp
D. .html
Answer: C. .cpp
Solution: .cpp is a common extension for C++ source files. Other extensions, such as .cc and .cxx, are also used.
cin and cout?A. stdio.h
B. iostream
C. string.h
D. math.h
Answer: B. iostream
Solution: The standard C++ header iostream declares the standard input and output stream objects, including std::cin and std::cout.
cout to display output?A. >>
B. <<
C. ==
D. &&
Answer: B. <<
Solution: The stream insertion operator << sends data to an output stream.
cin to read input?A. <<
B. >>
C. !=
D. %
Answer: B. >>
Solution: The stream extraction operator >> reads formatted input from a stream.
A. 2marks
B. total-marks
C. total_marks
D. class
Answer: C. total_marks
Solution: An identifier cannot begin with a digit, contain a hyphen as part of its name, or be a reserved keyword.
A. object
B. class
C. define
D. structure
Answer: B. class
Solution: The class keyword defines a user-defined class type.
A. A built-in operator
B. An instance of a class
C. A header file
D. A loop statement
Answer: B. An instance of a class
Solution: An object is an instance of a class or another object type.
A. Inheritance
B. Encapsulation
C. Recursion
D. Compilation
Answer: B. Encapsulation
Solution: Encapsulation groups data and related operations into a class and controls access to its implementation.
A. private
B. protected
C. public
D. hidden
Answer: C. public
Solution: Public members can be accessed wherever the object or class is accessible, subject to other language rules.
A. public
B. private
C. protected
D. static
Answer: B. private
Solution: Class members are private by default. For a struct, members are public by default.
A. malloc
B. create
C. new
D. alloc
Answer: C. new
Solution: The new expression allocates storage and initializes an object, returning a pointer to it when successful.
A. ->
B. .
C. ::
D. &
Answer: B. .
Solution: The dot operator accesses members through an object. The arrow operator -> is used through a pointer to an object.
A. .
B. ::
C. ->
D. **
Answer: C. ->
Solution: For a pointer p, p->member is equivalent to (*p).member.
A. A function that destroys an object
B. A special member function used to initialize objects
C. A loop used in a class
D. A type of header file
Answer: B. A special member function used to initialize objects
Solution: A constructor is invoked during object initialization and has the same name as its class.
A. A special member function used during object destruction
B. A function that creates a namespace
C. A variable initializer
D. An overloaded operator
Answer: A. A special member function used during object destruction
Solution: A destructor has the class name preceded by ~ and performs cleanup when an object's lifetime ends.
A. <!--
B. //
C. ##
D. **
Answer: B. //
Solution: C++ supports // single-line comments and /* ... */ block comments.
A. fixed
B. static
C. const
D. constant
Answer: C. const
Solution: const specifies that an object cannot be modified through that name after initialization, subject to the language's rules.
A. for
B. while
C. do-while
D. Range-based for
Answer: C. do-while
Solution: A do-while loop checks its condition after executing the body.
A. continue
B. break
C. goto
D. case
Answer: B. break
Solution: break exits the nearest enclosing loop or switch statement.
A. break
B. return
C. continue
D. switch
Answer: C. continue
Solution: continue moves control to the next iteration of the enclosing loop.
A. Defining several functions with the same name but different parameter lists
B. Defining a function inside a loop
C. Calling a function repeatedly
D. Defining two identical functions with identical signatures
Answer: A. Defining several functions with the same name but different parameter lists
Solution: Overloaded functions share a name but differ in their parameter lists. Return type alone cannot distinguish overloads.
A. Defining a variable twice
B. Providing a derived-class implementation of an inherited virtual function
C. Calling a constructor
D. Creating a template
Answer: B. Providing a derived-class implementation of an inherited virtual function
Solution: Overriding allows a derived class to provide its own implementation of a virtual member function.
A. static
B. virtual
C. friend
D. inline
Answer: B. virtual
Solution: A virtual function enables runtime polymorphism when called through a suitable base-class pointer or reference.
A. Encapsulation
B. Inheritance
C. Abstraction
D. Compilation
Answer: B. Inheritance
Solution: Inheritance establishes a relationship in which a derived class inherits accessible members from a base class.
A. generic
B. template
C. typename only
D. type
Answer: B. template
Solution: A template declaration begins with template. The parameter can be introduced using typename or class for a type parameter.
A. *
B. &
C. ->
D. .
Answer: B. &
Solution: The address-of operator returns the address of an appropriate object.
A. &
B. ::
C. *
D. .
Answer: C. *
Solution: Unary * accesses the object to which a valid pointer points.
A. error
B. catch
C. throws
D. finally
Answer: B. catch
Solution: C++ exception handling uses try, throw, and catch.
A. ofstream
B. ifstream
C. ostream
D. istringstream
Answer: B. ifstream
Solution: std::ifstream, declared in <fstream>, is used for file input.
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << x++;
return 0;
}
A. 4
B. 5
C. 6
D. Compilation error
Answer: B. 5
Solution: Post-increment returns the old value first and then increases x to 6.
int a = 10, b = 3;
cout << a / b;
A. 3.33
B. 3
C. 1
D. 4
Answer: B. 3
Solution: Both operands are integers, so integer division discards the fractional part.
int x = 4;
cout << (x > 3 ? 100 : 200);
A. 3
B. 4
C. 100
D. 200
Answer: C. 100
Solution: Since x > 3 is true, the conditional operator selects 100.
int x = 10;
if (x > 5)
cout << "A";
else if (x > 8)
cout << "B";
else
cout << "C";
A. A
B. B
C. AB
D. C
Answer: A. A
Solution: The first condition is true, so the remaining branches are skipped.
int x = 2;
switch (x) {
case 1: cout << "A"; break;
case 2: cout << "B";
case 3: cout << "C"; break;
default: cout << "D";
}
A. B
B. BC
C. BCD
D. C
Answer: B. BC
Solution: Execution begins at case 2. Because there is no break after cout << "B", execution falls through to case 3.
for (int i = 1; i <= 5; i++)
cout << i;
A. 4
B. 5
C. 6
D. Infinite times
Answer: B. 5
Solution: The loop executes for i = 1, 2, 3, 4, 5.
int i = 1;
while (i < 4) {
cout << i;
i++;
}
A. 123
B. 1234
C. 234
D. 111
Answer: A. 123
Solution: The loop stops when i becomes 4.
int a[] = {10, 20, 30, 40};
cout << a[2];
A. 10
B. 20
C. 30
D. 40
Answer: C. 30
Solution: Array indices begin at zero, so a[2] refers to the third element.
int x = 10;
void change(int n) {
n = 50;
}
// In main:
change(x);
cout << x;
A. 10
B. 50
C. 0
D. Compilation error
Answer: A. 10
Solution: C++ passes arguments by value by default. The parameter n is a copy of x.
void change(int &n) {
n = 50;
}
// In main:
int x = 10;
change(x);
cout << x;
A. 10
B. 50
C. 0
D. Undefined behavior
Answer: B. 50
Solution: The parameter int &n is a reference to the original variable, so changing n changes x.
A. int sum(int a, int b);
B. int sum(int a, int b = 10);
C. int sum(void);
D. int sum(int =);
Answer: B. int sum(int a, int b = 10);
Solution: A caller can omit b, in which case its default value of 10 is used.
int add(int a, int b) {
return a + b;
}
int main() {
cout << add(4, 6);
}
A. 4
B. 6
C. 10
D. 46
Answer: C. 10
Solution: The function adds 4 and 6 and returns 10.
int square(int x) {
return x * x;
}
int main() {
cout << square(5);
}
A. 10
B. 15
C. 20
D. 25
Answer: D. 25
Solution: The function calculates \(5 \times 5 = 25\).
A. They must always be expanded inline by the compiler.
B. They cannot have parameters.
C. The inline specifier does not guarantee inline machine-code expansion.
D. They cannot return a value.
Answer: C. The inline specifier does not guarantee inline machine-code expansion.
Solution: inline has language-level implications, including allowing suitable definitions to appear in multiple translation units. The compiler decides whether to inline a call.
class Test {
public:
int x;
Test() {
x = 10;
}
};
int main() {
Test t;
cout << t.x;
}
A. 0
B. 10
C. Garbage value
D. Compilation error
Answer: B. 10
Solution: The constructor assigns 10 to x when object t is created.
class Demo {
public:
Demo() {
cout << "A";
}
~Demo() {
cout << "B";
}
};
int main() {
Demo d;
}
A. AB
B. BA
C. A
D. B
Answer: A. AB
Solution: Construction prints A. The destructor prints B when the local object is destroyed as main() exits.
A. Using multiple destructors in one class
B. Defining multiple constructors with different parameter lists
C. Calling a constructor twice automatically
D. Inheriting a constructor's return type
Answer: B. Defining multiple constructors with different parameter lists
Solution: Constructor overloading allows objects to be initialized in different ways.
class A {
public:
int x;
A(int n) : x(n) {}
};
int main() {
A a(25);
cout << a.x;
}
A. 0
B. 1
C. 25
D. Compilation error
Answer: C. 25
Solution: The constructor's member initializer list initializes x to 25.
A. public
B. private
C. protected
D. static
Answer: C. protected
Solution: Protected members are accessible within the class and its eligible derived classes, subject to C++ access rules.
A. Multiple inheritance
B. Single inheritance
C. Hybrid inheritance
D. Hierarchical inheritance
Answer: B. Single inheritance
Solution: Single inheritance has one direct base class and one derived class.
A. Single inheritance
B. Multilevel inheritance
C. Multiple inheritance
D. Hierarchical inheritance
Answer: C. Multiple inheritance
Solution: Multiple inheritance allows one derived class to have multiple direct base classes.
A. Macros
B. Virtual functions
C. Header guards
D. The sizeof operator
Answer: B. Virtual functions
Solution: Virtual functions support dynamic dispatch through base-class pointers or references.
A. A function that has no return type
B. A virtual function declared with = 0
C. A private constructor
D. A static function
Answer: B. A virtual function declared with = 0
Solution: For example, virtual void show() = 0; declares a pure virtual function. A class with an unimplemented pure virtual function remains abstract.
A. A class that cannot be instantiated directly
B. A class without data members
C. A class containing only static variables
D. A class with no constructors
Answer: A. A class that cannot be instantiated directly
Solution: A class with at least one pure virtual function that has not been overridden remains abstract.
this pointer refer to in a non-static member function?A. The base class
B. The current object
C. The next object in memory
D. The global namespace
Answer: B. The current object
Solution: this provides access to the object on which a non-static member function is operating.
A. virtual
B. friend
C. inline
D. extern
Answer: B. friend
Solution: A friend function is not a member of the class, but it receives the access rights granted by the friendship declaration.
A. To allocate dynamic memory
B. To group names and reduce naming conflicts
C. To define constructors only
D. To terminate a program
Answer: B. To group names and reduce naming conflicts
Solution: Namespaces organize identifiers, such as functions and classes, under a named scope.
sizeof(int) return?A. Always 2
B. Always 4
C. The size of an int in bytes on the implementation
D. The number of digits in an integer
Answer: C. The size of an int in bytes on the implementation
Solution: The size depends on the implementation. C++ guarantees that sizeof(char) is 1 byte, but the size of int is implementation-dependent.
A. A reference must be initialized when declared.
B. A reference can always be made to refer to a different object later.
C. References cannot be used as function parameters.
D. References are identical to pointers in all respects.
Answer: A. A reference must be initialized when declared.
Solution: A reference must be bound to an appropriate object or function when initialized and cannot later be reseated to refer to a different object.
std::string?A. It is a built-in C array type.
B. It is a standard C++ class for managing strings.
C. It can contain only one character.
D. It requires stdio.h.
Answer: B. It is a standard C++ class for managing strings.
Solution: std::string is provided by the <string> header and supports operations such as concatenation, comparison, and length retrieval.
std::vector?A. <array>
B. <vector>
C. <list.h>
D. <iostream>
Answer: B. <vector>
Solution: The <vector> header provides the dynamically resizable sequence container std::vector.
new and delete is correct?A. delete allocates memory.
B. new allocates and initializes an object, while delete destroys it and releases its storage.
C. new and delete are C preprocessor directives.
D. delete must be used for every local variable.
Answer: B. new allocates and initializes an object, while delete destroys it and releases its storage.
Solution: Dynamically allocated objects created with a single-object new expression are normally released with the corresponding delete expression.
A. catch
B. try
C. throw
D. raise
Answer: C. throw
Solution: The throw expression signals an exception that can be handled by a matching catch handler.
A. ifstream
B. ofstream
C. istream
D. cin
Answer: B. ofstream
Solution: std::ofstream, declared in <fstream>, provides file output operations.
getline() commonly do when used with a C++ input stream and a std::string?A. Reads one integer only
B. Reads a line of text, typically up to a newline delimiter
C. Writes text to a file
D. Allocates a character pointer manually
Answer: B. Reads a line of text, typically up to a newline delimiter
Solution: std::getline() reads characters from a stream into a string until the delimiter is encountered or input ends.
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << ++x << " " << x++;
return 0;
}
A. 5 6
B. 6 6
C. 6 7
D. 7 7
Answer: C. 6 7
Solution: ++x first increments x to 6 and prints it. Then x++ prints 6 and increments it to 7. Correction: the actual output is 6 6, because the post-increment expression yields the old value before the increment.
int x = 5;
int y = x++;
cout << x << " " << y;
A. 5 5
B. 6 5
C. 6 6
D. 5 6
Answer: B. 6 5
Solution: The old value 5 is assigned to y, and then x becomes 6.
int x = 10;
if (x = 0)
cout << "True";
else
cout << "False";
A. True
B. False
C. 10
D. Compilation error
Answer: B. False
Solution: x = 0 is an assignment, not a comparison. The expression evaluates to zero, which is false.
int a = 5;
int b = 2;
cout << (a / b) << " " << (a % b);
A. 2 1
B. 2.5 1
C. 3 0
D. 2 0
Answer: A. 2 1
Solution: Integer division gives 2, and the remainder operator gives 1.
int x = 5;
cout << (x << 1);
A. 5
B. 10
C. 20
D. 2
Answer: B. 10
Solution: Left-shifting the positive integer 5 by one bit produces 10.
int x = 5;
int *p = &x;
*p = 20;
cout << x;
A. 5
B. 20
C. Address of x
D. Compilation error
Answer: B. 20
Solution: The pointer p refers to x. Assigning through *p changes the original variable.
int a[] = {10, 20, 30, 40};
int *p = a;
cout << *(p + 2);
A. 10
B. 20
C. 30
D. 40
Answer: C. 30
Solution: Pointer arithmetic advances by elements of the pointed-to type. p + 2 points to a[2].
int x = 10;
int &r = x;
r = 50;
cout << x;
A. 10
B. 50
C. Address of x
D. Compilation error
Answer: B. 50
Solution: r is a reference to x, not a separate variable. Assigning to r changes x.
class Demo {
public:
int x = 10;
void show() {
cout << this->x;
}
};
int main() {
Demo d;
d.show();
}
A. 0
B. 10
C. Address of d
D. Compilation error
Answer: B. 10
Solution: this points to the current object. Therefore, this->x accesses the member x of d.
class Test {
public:
static int count;
};
int Test::count = 5;
int main() {
Test a, b;
a.count++;
cout << b.count;
}
A. 5
B. 6
C. 0
D. Compilation error
Answer: B. 6
Solution: A static data member belongs to the class rather than to each individual object. Both a.count and b.count refer to the same variable.
class A {
public:
A() {
cout << "A";
}
};
class B : public A {
public:
B() {
cout << "B";
}
};
int main() {
B obj;
}
A. AB
B. BA
C. B
D. A
Answer: A. AB
Solution: The base-class constructor runs before the derived-class constructor.
class A {
public:
void show() {
cout << "A";
}
};
class B : public A {
public:
void show() {
cout << "B";
}
};
int main() {
B obj;
obj.show();
}
A. A
B. B
C. AB
D. Compilation error
Answer: B. B
Solution: The derived class defines its own show() function, which hides the base-class function of the same name for this call.
class A {
public:
virtual void show() {
cout << "A";
}
};
class B : public A {
public:
void show() override {
cout << "B";
}
};
int main() {
B obj;
A *p = &obj;
p->show();
}
A. A
B. B
C. AB
D. Compilation error
Answer: B. B
Solution: Because show() is virtual, the function called is determined by the actual object type at runtime.
class A {
public:
virtual void show() {
cout << "A";
}
};
class B : public A {
public:
void show() override {
cout << "B";
}
};
int main() {
B obj;
A &ref = obj;
ref.show();
}
A. A
B. B
C. AB
D. Compilation error
Answer: B. B
Solution: A base-class reference can refer to a derived-class object. Virtual dispatch invokes the overridden function in B.
A. It prevents an object from being destroyed.
B. It supports correct destruction through a base-class pointer when deleting a derived object.
C. It makes every member function virtual.
D. It can take arbitrary parameters.
Answer: B. It supports correct destruction through a base-class pointer when deleting a derived object.
Solution: When a derived object may be deleted through a base-class pointer, the base class should generally have a virtual destructor.
class A {
public:
A() {
cout << "A";
}
~A() {
cout << "B";
}
};
int main() {
A obj;
}
A. AB
B. BA
C. AA
D. BB
Answer: A. AB
Solution: The constructor prints A when the object is created. The destructor prints B when the local object leaves scope.
class Number {
public:
int x;
Number(int n) : x(n) {}
Number operator+(const Number& other) {
return Number(x + other.x);
}
};
int main() {
Number a(10), b(20);
Number c = a + b;
cout << c.x;
}
A. 10
B. 20
C. 30
D. Compilation error
Answer: C. 30
Solution: The overloaded + operator returns a new Number whose value is the sum of the two operands.
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << maximum(10, 25);
}
A. 10
B. 25
C. 35
D. Compilation error
Answer: B. 25
Solution: The compiler instantiates the function template for int. The function returns the greater of the two values.
template <typename T>
T square(T x) {
return x * x;
}
int main() {
cout << square(4);
}
A. 8
B. 12
C. 16
D. 20
Answer: C. 16
Solution: The compiler deduces T as int, and the function returns \(4 \times 4 = 16\).
A. It can always be instantiated directly.
B. It cannot be used as a base class.
C. It cannot be instantiated directly, but pointers and references to it can be used.
D. It cannot have constructors.
Answer: C. It cannot be instantiated directly, but pointers and references to it can be used.
Solution: Abstract classes provide interfaces for derived classes. They can have constructors, data members, and implemented member functions.
class Base {
public:
int x = 10;
};
class Derived : public Base {
public:
int y = 20;
};
int main() {
Derived d;
cout << d.x + d.y;
}
A. 10
B. 20
C. 30
D. Compilation error
Answer: C. 30
Solution: Derived publicly inherits Base, so its object has access to the public member x and its own member y.
A. The first base class is always selected.
B. The second base class is always selected.
C. The name may be ambiguous, causing a compilation error.
D. The compiler combines both members automatically.
Answer: C. The name may be ambiguous, causing a compilation error.
Solution: In multiple inheritance, an unqualified member name can be ambiguous. Explicit qualification, such as obj.Base1::show(), can resolve the ambiguity.
class A {
public:
void show() {
cout << "A";
}
};
class B {
public:
void display() {
cout << "B";
}
};
class C : public A, public B {
};
int main() {
C obj;
obj.show();
obj.display();
}
A. A
B. B
C. AB
D. Compilation error
Answer: C. AB
Solution: Class C inherits the accessible members of both A and B, so both calls are valid.
malloc() and new in C++ is correct?A. Both always call constructors.
B. new initializes objects according to the new-expression rules; malloc() only allocates raw storage.
C. malloc() automatically releases memory.
D. new must be declared in <stdio.h>.
Answer: B. new initializes objects according to the new-expression rules; malloc() only allocates raw storage.
Solution: new performs allocation and object initialization. malloc() allocates raw memory without invoking a C++ object's constructor.
A. new with free()
B. malloc() with delete
C. new with delete, and new[] with delete[]
D. new[] with delete in every case
Answer: C. new with delete, and new[] with delete[]
Solution: Use matching allocation and deallocation forms. Mismatching them can cause undefined behavior.
int x = 10;
int *p = &x;
cout << *p + 5;
A. 5
B. 10
C. 15
D. Address of x
Answer: C. 15
Solution: *p evaluates to 10. Adding 5 produces 15.
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Computer";
s += " Science";
cout << s.length();
}
A. 8
B. 14
C. 15
D. 16
Answer: C. 15
Solution: "Computer" has 8 characters and " Science" has 8 characters, including the leading space. The correct total is 16, so the correct option is D.
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World";
cout << s.substr(6, 5);
}
A. Hello
B. World
C. World
D. ello
Answer: B. World
Solution: substr(6, 5) extracts five characters starting at index 6, which produces "World".
try {
throw 10;
}
catch (int x) {
cout << x;
}
A. Prints 0
B. Prints 10
C. Prints int
D. Compilation error
Answer: B. Prints 10
Solution: The throw expression throws an integer exception. The matching catch (int x) handler receives its value.
try {
throw 3.14;
}
catch (int x) {
cout << "Integer";
}
catch (double x) {
cout << "Double";
}
A. Integer
B. Double
C. IntegerDouble
D. Compilation error
Answer: B. Double
Solution: The thrown value is a double, so the second handler matches. The first handler is skipped.
A. Every exception must be an integer.
B. A matching catch handler can handle a thrown exception.
C. try always prevents errors from occurring.
D. A catch block must appear before try.
Answer: B. A matching catch handler can handle a thrown exception.
Solution: C++ exception handling transfers control to a matching handler when an exception is thrown and a suitable handler is found.
#include <iostream>
using namespace std;
int main() {
int a[3] = {10, 20, 30};
for (int i = 2; i >= 0; i--)
cout << a[i] << " ";
return 0;
}
A. 10 20 30
B. 30 20 10
C. 30 10 20
D. 20 30 10
Answer: B. 30 20 10
Solution: The loop accesses the array from index 2 down to index 0.
class A {
public:
int x;
A() : x(10) {}
A(const A& other) : x(other.x + 5) {}
};
int main() {
A a;
A b = a;
cout << b.x;
}
A. 5
B. 10
C. 15
D. Compilation error
Answer: C. 15
Solution: The copy constructor initializes b.x to a.x + 5, giving \(10 + 5 = 15\).
#include <iostream>
using namespace std;
class Test {
public:
Test() {
cout << "C";
}
Test(const Test&) {
cout << "K";
}
};
int main() {
Test a;
Test b = a;
return 0;
}
A. CC
B. CK
C. KC
D. KK
Answer: B. CK
Solution: Constructing a calls the default constructor and prints C. Initializing b from a calls the copy constructor and prints K.
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base";
}
virtual ~Base() = default;
};
class Derived : public Base {
public:
void show() override {
cout << "Derived";
}
};
int main() {
Base *p = new Derived();
p->show();
delete p;
return 0;
}
A. Base
B. Derived
C. BaseDerived
D. Compilation error
Answer: B. Derived
Solution: Virtual dispatch calls Derived::show() because the actual object is a Derived. The virtual destructor also ensures correct destruction when deleting the object through a Base*.