C++ Preparation SJVN
Questions
Section titled “Questions”Here’s a set of practice questions for C/C++ that cover common competitive/programming exam topics:
Expression Evaluation & Operators
- What is the value of
int a = 5; int b = a++ + ++a;? - Evaluate:
(3 && 0) || (2 || 0) - What is the output of
printf("%d", 10 & 3 | 2);?
Variables & Scope
4. Difference between auto, register, static, and extern variables.
5. What will be printed if a local static variable is incremented in a function called multiple times?
Constants & Literals
6. Which of the following is a valid octal constant: 075, 089, 0x1F?
7. How do you define a long double constant?
8. Difference between const int x = 10; and #define X 10.
Arrays & Pointers
9. What is the difference between char *p and char arr[]?
10. What is printed by printf("%d", sizeof(arr)/sizeof(arr[0]));?
11. Can you increment a pointer to a multi-dimensional array?
Strings & I/O
12. Difference between gets(), fgets(), and scanf("%s").
13. What happens if you forget \0 at the end of a string?
14. Explain strlen, strcpy, strcmp with examples.
Functions & Recursion
15. Difference between call by value and call by reference.
16. Write a recursive function to calculate factorial.
17. What is a function pointer and how is it used?
Control Statements
18. Difference between while and do-while.
19. What is the output of nested loops with break and continue?
20. Explain switch-case fallthrough with an example.
Preprocessor & Macros
21. Difference between #include <file> and #include "file".
22. What is the output of using #define SQR(x) x*x in SQR(1+2)?
Miscellaneous
23. Difference between NULL, nullptr, and 0 in C++.
24. What is the result of sizeof(char*), sizeof(int*) on a 64-bit machine?
25. Explain short-circuit evaluation with && and ||.
Answers
Section titled “Answers”Expression Evaluation & Operators
int a = 5; int b = a++ + ++a;- Answer:
b = 12 - Explanation:
a++uses5, thenabecomes6;++aincrements to7then uses it →5 + 6? Actually compute step by step:a = 5a++= 5,a = 6++a= 7,a = 7b = 5 + 7 = 12✅ Correction: b = 12
- Answer:
(3 && 0) || (2 || 0)- Answer:
1 - Explanation:
3 && 0→0(false)2 || 0→1(true)0 || 1→1
- Answer:
printf("%d", 10 & 3 | 2);- Answer:
2 - Explanation:
10 & 3→1010 & 0011=0010= 2 ⭐2 | 2→2
- Answer:
Variables & Scope
-
auto,register,static,extern- Answer / Explanation:
auto→ default local variableregister→ hint to store in CPU registerstatic→ retains value, scope limited (local static) or file-level (global static)extern→ global variable declared in another file
- Answer / Explanation:
-
Local static variable incremented multiple times
void fun() {static int x = 0;x++;printf("%d ", x);}- Answer:
1 2 3 ...on repeated calls - Explanation:
staticretains value across function calls - Even if you write
static int x = 0;inside the function, the initialization happens only once, when the program first encounters the function. On subsequent calls,xkeeps its previous value; it is not re-initialized.
- Answer:
Constants & Literals 6. Valid octal constant: 075 ✅, 089 ❌, 0x1F ❌ (hexadecimal)
-
Long double constant:
long double x = 3.14159L; -
Difference:
const int x = 10;→ type-checked, read-only#define X 10→ preprocessor text substitution, no type checking
Arrays & Pointers 9. char *p vs char arr[] - char *p → pointer, can point elsewhere - char arr[] → actual array in memory, cannot be reassigned
sizeof(arr)/sizeof(arr[0])→ number of elements in array- Pointer increment in multi-dimensional array: yes, pointer arithmetic depends on row/column layout
Strings & I/O
gets()→ reads until newline, unsafe
fgets()→ reads max n chars, includes newline, safe
scanf("%s")→ reads until whitespace- Forget
\0→ undefined behavior when using string functions strlen→ length of string
strcpy→ copy string
strcmp→ compare strings
Functions & Recursion
- Call by value → copies value, original unchanged
Call by reference → modifies original variable - Factorial (recursive):
int fact(int n){ return (n<=1)?1:n*fact(n-1); }- Function pointer: stores address of function ⭐
int (*fp)(int,int) = &add; fp(2,3);Control Statements 18. while → check condition before execution
do-while → executes at least once
- Nested loops:
breakexits loop,continueskips current iteration - Switch-case fallthrough: no
break→ executes next case
Preprocessor & Macros
#include <file>→ system headers
#include "file"→ user-defined files#define SQR(x) x*x; SQR(1+2)→ expands to1+2*1+2 = 5❌ (wrong math if not parenthesized) ⭐⭐
Miscellaneous
NULL→ 0 in C,nullptr→ C++11 null pointer,0→ integer literal usable as nullsizeof(char*) = 8,sizeof(int*) = 8(on 64-bit machine)- Short-circuit:
&&stops if first is false,||stops if first is true