optional · CSS study notes
Question Bank · Chapter II (2016–2026)
⬆️ Back to: P-I.II. Programming Fundamentals
🧭
Chapter II question bank, organised like Chapter I: year-wise index → quick revision → worked answers → MCQs. Contains 47 existing answer blocks covering 48 written question references, plus 7 MCQ records, recovered from the 15 topic pages. This is an inventory of your Notion notes, not a fresh audit of the original examination PDFs. Questions may be paraphrased; marks and references are retained as recorded.
1. Written-question index
| Recorded reference | Question / task | Topic answer |
|---|---|---|
| 2016 · Paper-I · Q2(a) · 6 marks | Write a program to input a choice for temperature conversion (Fahrenheit to Celsius or Celsius to Fahrenheit), then input the temperature and display the answer. | II.11 Control Structures |
| 2016 · Paper-I · Q3(a) · 6 marks | Define a function monthly_profit taking a percent profit rate and an investment amount (floats) and returning the monthly profit. Write the prototype, definition and a main program. | II.12 Functions & Parameter Passing |
| 2016 · Paper-I · Q4(a) · 6 marks | Define a structure for a Person (name, age, address). Input one record and display "Young" if age < 20, "Old" if age > 50, otherwise "Middle". | II.15 Structures, Unions, and Bit Manipulation |
| 2016 · Paper-I · Q7(a) · 5 marks | Draw a diagram of translating a C++ source file into an executable. Give an example of each phase. | II.7 Source to Exe File Conversion Process (2016) |
| 2017 · Paper-I · Q3(a) · 8 marks | Elucidate the three types of programming languages (machine, low-level, high-level), giving details of how low-level and high-level language are converted into machine language. | II.5 Types of Programming Languages (2017) |
| 2017 · Paper-I · Q3(b) · 6 marks | Write a function to calculate the quadratic equation roots x = (−b ± √(b²−4ac)) / 2a using the math library. | II.12 Functions & Parameter Passing |
| 2017 · Paper-I · Q3(c) · 6 marks | Write a supermarket checkout program. The database of items (name, code, price) is a structure. The cashier enters codes and quantities, and the program prints the total. | II.15 Structures, Unions, and Bit Manipulation |
| 2017 · Paper-I · Q8(a) · 5 marks | Write a short note on global, local and shared variables for nested function access. | II.9 Global, Local and Shared Variables (2017) |
| 2018 · Paper-I · Q2(d) · 3 marks | List any four best practices for coding standards. | II.6 Program Development and Execution |
| 2018 · Paper-I · Q3(a) · 5 marks | Write a grade average calculator. Input marks for five subjects, output the average and an appropriate message. | II.11 Control Structures |
| 2018 · Paper-I · Q3(b) · 2 marks | Write the conditions: (i) print hello when i is twice j, or j is smaller than k and n, or m is negative; (ii) print hello when i is not in the interval 6–9. | II.11 Control Structures |
| 2018 · Paper-I · Q3(c) · 1 mark | Write an equivalent instruction to w += 2*z + 4; without the += operator. | II.1 Basic Programming Elements and Concepts |
| 2018 · Paper-I · Q3(d) · 2 marks | Predict the values of a and b after every instruction: a=5, b=6; a=(b++)+3; b=--a; | II.1 Basic Programming Elements and Concepts |
| 2018 · Paper-I · Q3(e) · 5 marks | Print a triangle of increasing numbers (for n = 4: 1 2 3 4 / 1 2 3 / 1 2 / 1). | II.11 Control Structures |
| 2018 · Paper-I · Q3(f) · 5 marks | Replace the character at position pos in string st with ch (for "helloworld", position 2 and i gives "hilloworld"). | II.14 Arrays, Pointers, and Strings |
| 2018 · Paper-I · Q5(a) · 3 marks | What is a dangling pointer? | II.14 Arrays, Pointers, and Strings |
| 2019 · Paper-I · Q3(a) · 10 marks | Identify the logical errors in the prime-number program and give the correct statement at the same line number. | II.11 Control Structures |
| 2019 · Paper-I · Q3(b) · 5 marks | What is the difference between call by value and call by reference? | II.12 Functions & Parameter Passing |
| 2019 · Paper-I · Q3(c) · 5 marks | What is the role of preprocessor directives? Give three examples in C++. | II.8 Program Structure & Data Types |
| 2019 · Paper-I · Q5(b) · 5 marks and 2023 · Paper-I · Q3(c) · 6 marks | What is bit twiddling? Give a brief description. | II.15 Structures, Unions, and Bit Manipulation |
| 2020 · Paper-I · Q2(a) · 8 marks | Write a C/C++ program implementing binary logical AND, OR and NOT gates. | II.11 Control Structures |
| 2020 · Paper-I · Q2(b) · 7 marks | Input a number and print the Fibonacci series up to it. | II.11 Control Structures |
| 2020 · Paper-I · Q4(c) · 4 marks | Convert numbers into words (123 → one hundred and twenty three), continuing until the user types quit. | II.11 Control Structures |
| 2021 · Paper-I · Q3(b) · 5 marks | Write a program that outputs the factors of a number, one per line. | II.11 Control Structures |
| 2021 · Paper-I · Q3(c) · 5 marks | What are IDEs? How do they help in software development? List the IDEs you have used. | II.6 Program Development and Execution |
| 2021 · Paper-I · Q3(d) · 4 marks | Write a C/C++ program to convert a decimal number to hexadecimal. | II.11 Control Structures |
| 2021 · Paper-I · Q4(c) · 6 marks | What do you mean by runtime and compile-time errors? | II.6 Program Development and Execution |
| 2021 · Paper-I · Q5(b) · 6 marks | What is pass by value and pass by reference? Can we pass an object as a parameter to a method in Java? | II.12 Functions & Parameter Passing |
| 2022 · Paper-I · Q2(a) · 8 marks | Write a function pattern(int n) that prints the right-aligned number triangle (1 / 1 2 / 1 2 3 …). | II.11 Control Structures |
| 2022 · Paper-I · Q2(c) · 6 marks | Write the output: int ary[4]={1,2,3,4}; int *p1=ary+3; cout<<p1[-2]<<endl; int *p2=&p1[-2]; *p2=10; cout<<ary[1]<<endl; | II.14 Arrays, Pointers, and Strings |
| 2022 · Paper-I · Q3(c) · 6 marks | Write the output: int v1=55; int* p1=&v1; int* p2; p2=p1; *p1=20; cout<<*p2<<endl; cout<<v1<<endl; | II.14 Arrays, Pointers, and Strings |
| 2022 · Paper-I · Q4(a) · 12 marks | Write a C++ program to check whether a string is a palindrome without string library functions, taking input with cin.getline. | II.14 Arrays, Pointers, and Strings |
| 2022 · Paper-I · Q4(b) · 8 marks | Write the output of the program with a = 12, b = 25 that prints `a | b`. |
| 2022 · Paper-I · Q6(a) · 8 marks | Complete the recursive function int power(int k, int n) to calculate kⁿ without library functions. | II.12 Functions & Parameter Passing |
| 2023 · Paper-I · Q3(a) · 8 marks | How is dynamic memory allocation managed in a C++ program? Clarify with a program. | II.14 Arrays, Pointers, and Strings |
| 2024 · Paper-I · Q3(a) · 6 marks | Prompt for a letter grade A, B, C, D or F and display 4, 3, 2, 1 or 0. | II.11 Control Structures |
| 2024 · Paper-I · Q3(b) · 6 marks | Write pseudocode or C for the dot product a·b = Σ aᵢbᵢ. | II.11 Control Structures |
| 2024 · Paper-I · Q4(a) · 6 marks | Display the area and perimeter of a rectangle with width 4.5 and height 7.9. | II.11 Control Structures |
| 2024 · Paper-I · Q4(b) · 6 marks | Read a Celsius degree and display the Fahrenheit value using F = (9/5) × C + 32. | II.1 Basic Programming Elements and Concepts |
| 2024 · Paper-I · Q4(c) · 8 marks | Enter a month and year and display the number of days in that month. | II.11 Control Structures |
| 2024 · Paper-I · Q5(a) · 6 marks | Show the output of the Java code: Double x = 3.5; System.out.println(x.intValue()); System.out.println(x.compareTo(4.5)); | II.13 Standard Library Functions |
| 2025 · Paper-I · Q4(a) · 6 marks | Output the arithmetic series of n terms and its sum Sₙ = (n/2)[2a + (n−1)d], with a = 5, d = 3, n = 6. | II.11 Control Structures |
| 2025 · Paper-I · Q4(b) · 6 marks | Write a C++ program to calculate and display the product of two 2 × 2 matrices. | II.1 Basic Programming Elements and Concepts |
| 2025 · Paper-I · Q4(c) · 8 marks | Demonstrate two complex numbers using a structure. Use a function to add them and display the result as (a + bi). | II.15 Structures, Unions, and Bit Manipulation |
| 2026 · Paper-I · Q3(a) · 6 marks | What is bit manipulation in programming, and why is it used? Give two examples. | II.15 Structures, Unions, and Bit Manipulation |
| 2026 · Paper-I · Q3(b) · 6 marks | What are arrays? Describe one advantage and one limitation. | II.14 Arrays, Pointers, and Strings |
| 2026 · Paper-I · Q3(c) · 8 marks | Write pseudocode to reverse an array using pointers. | II.14 Arrays, Pointers, and Strings |
2. Quick revision and worked answers
Short code fragments below are solution bodies, not standalone programs: add the relevant headers, namespace qualification and enclosing main/function when a complete program is requested. Follow the original paper's input ranges and exact output format. Original-paper line numbers are not reverified here.
2016 · Paper-I · Q2(a) · 6 marks
Q. Write a program to input a choice for temperature conversion (Fahrenheit to Celsius or Celsius to Fahrenheit), then input the temperature and display the answer.
Quick revision — key points
- Read the conversion choice; choose the matching formula; preserve floating-point division; test both branches.
Worked answer
#include <iostream>
using namespace std;
int main() {
int choice; float t;
cout << "1. Fahrenheit to Celsius\n2. Celsius to Fahrenheit\nEnter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "Enter Temperature in Fahrenheit: "; cin >> t;
cout << "Same Temperature in Celsius: " << (t - 32) * 5 / 9;
} else if (choice == 2) {
cout << "Enter Temperature in Celsius: "; cin >> t;
cout << "Same Temperature in Fahrenheit: " << t * 9 / 5 + 32;
} else cout << "Invalid choice";
return 0;
}
- Check: 98°F gives (98 − 32) × 5/9 = 36.7°C.
Source: II.11 Control Structures
2016 · Paper-I · Q3(a) · 6 marks
Q. Define a function monthly_profit taking a percent profit rate and an investment amount (floats) and returning the monthly profit. Write the prototype, definition and a main program.
Quick revision — key points
- State whether the quoted rate is monthly or annual; declare the prototype; define the calculation; call it from main.
Worked answer
#include <iostream>
using namespace std;
float monthlyProfit(float rate, float amount); // prototype
int main() {
float r, a;
cout << "Enter percent profit rate: "; cin >> r;
cout << "Enter investment amount: "; cin >> a;
cout << "Monthly profit: " << monthlyProfit(r, a);
return 0;
}
float monthlyProfit(float rate, float amount) { // definition
return (rate / 100) * amount;
}
- Check: (8/100) × 50000 = 4000.
Source: II.12 Functions & Parameter Passing
Rate-period ambiguity: The topic page treats the input as a monthly percentage; the older Most Repeated answer divides an annual rate by 12. Use amount × rate / 100 for a monthly rate, or amount × annualRate / (100 × 12) for a simple annual rate. Check the original wording before choosing.
2016 · Paper-I · Q4(a) · 6 marks
Q. Define a structure for a Person (name, age, address). Input one record and display "Young" if age < 20, "Old" if age > 50, otherwise "Middle".
Quick revision — key points
- Define the record fields; input the complete record; test age boundaries; display the category.
Worked answer
struct Person { char name[30]; int age; char address[50]; };
int main() {
Person p;
cout << "Enter Name = "; cin.getline(p.name, 30);
cout << "Enter Age = "; cin >> p.age; cin.ignore();
cout << "Enter Address = "; cin.getline(p.address, 50);
if (p.age < 20) cout << "Person is YOUNG";
else if (p.age > 50) cout << "Person is OLD";
else cout << "Person is MIDDLE";
}
- The paper leaves ages 20 and 50 undefined, so treat them as Middle.
Source: II.15 Structures, Unions, and Bit Manipulation
2016 · Paper-I · Q7(a) · 5 marks
Q. Draw a diagram of translating a C++ source file into an executable. Give an example of each phase.
Quick revision — key points
- Show source → preprocessing → compilation → assembly → linking; loading happens after the executable exists.
Worked answer
Source (.cpp) → Preprocessor → Compiler → Assembler → Linker → Loader → Running program
- Preprocessor: expands
#includeand#define(the iostream header text is inserted). - Compiler: checks syntax and meaning, then produces assembly code (
x = a + b;becomes load, add and store instructions). - Assembler: turns assembly into machine-code object file (
.objor.o). - Linker: combines object files and libraries (such as the standard library for
cout) into an executable (.exe). - Loader: places the executable in memory and starts it.
Source: II.7 Source to Exe File Conversion Process (2016)
2017 · Paper-I · Q3(a) · 8 marks
Q. Elucidate the three types of programming languages (machine, low-level, high-level), giving details of how low-level and high-level language are converted into machine language.
Quick revision — key points
- Compare abstraction, portability and translation; distinguish assembler, compiler and interpreter.
Worked answer
- Machine language: CPU instructions specific to an architecture; the CPU executes them directly. Speed depends on the actual instructions and algorithm.
- Low-level (assembly) language: symbolic mnemonics such as
ADD,MOV. It is close to hardware, easier than binary, and machine-dependent. - High-level language: English-like, portable and machine-independent (C++, Java, Python). It is easy to write but needs translation.
- Conversion of assembly: an assembler converts each mnemonic into machine code (one-to-one).
- Conversion of high-level code:
- a compiler translates the whole program into object code through lexical, syntax and semantic analysis, intermediate code, optimisation and code generation, and the linker then produces the executable
- Interpretation: a runtime executes source or an intermediate representation; literal line-by-line execution is only a simplified model.
- Comparison: abstraction and hardware control differ; neither language level nor generation guarantees execution speed.
Source: II.5 Types of Programming Languages (2017)
2017 · Paper-I · Q3(b) · 6 marks
Q. Write a function to calculate the quadratic equation roots x = (−b ± √(b²−4ac)) / 2a using the math library.
Quick revision — key points
- Check a ≠ 0; compute the discriminant; distinguish positive, zero and negative cases; call the function.
Worked answer
#include <iostream>
#include <cmath>
using namespace std;
void quadratic(float a, float b, float c) {
float d = b * b - 4 * a * c;
if (a == 0) { cout << "Not a quadratic equation"; return; }
if (d < 0) { cout << "Roots are complex"; return; }
cout << "The values of x are " << (-b + sqrt(d)) / (2 * a)
<< ", " << (-b - sqrt(d)) / (2 * a);
}
int main() {
float a, b, c;
cout << "Enter value of a: "; cin >> a;
cout << "Enter value of b: "; cin >> b;
cout << "Enter value of c: "; cin >> c;
quadratic(a, b, c);
}
- Check: a = 1, b = 2, c = 1 gives −1, −1.
Source: II.12 Functions & Parameter Passing
2017 · Paper-I · Q3(c) · 6 marks
Q. Write a supermarket checkout program. The database of items (name, code, price) is a structure. The cashier enters codes and quantities, and the program prints the total.
Quick revision — key points
- Store code, name and price together; find the code; multiply price by quantity; accumulate the bill.
Worked answer
struct Item { int code; char name[20]; float price; };
Item db[3] = { {1,"Rice",500}, {2,"Sugar",150}, {3,"Oil",400} };
float total = 0; char more;
do {
int code, qty;
cout << "Enter product code: "; cin >> code;
cout << "Enter quantity: "; cin >> qty;
for (int i = 0; i < 3; i++)
if (db[i].code == code) total += db[i].price * qty;
cout << "Do you have more products to add (Y/N): "; cin >> more;
} while (more == 'Y' || more == 'y');
cout << "The total amount is " << total;
- To search by name, compare with
strcmp(db[i].name, input) == 0.
Source: II.15 Structures, Unions, and Bit Manipulation
2017 · Paper-I · Q8(a) · 5 marks
Q. Write a short note on global, local and shared variables for nested function access.
Quick revision — key points
- Separate visibility from lifetime; explain shadowing; distinguish shared state from ordinary local variables.
Worked answer
- Global variable: declared outside all functions, visible to every function in the file, and lives for the whole program. Too many globals make code hard to debug.
- Local variable: declared inside a function or block, visible only there, and created and destroyed on each call. A
staticlocal keeps its value between calls. - Shared variable: a global used by several functions or files (declared with
externin other files). Concurrent access needs protection (locks). - Nested functions: C and C++ do not allow a function inside a function (only as a compiler extension). Languages such as Pascal and Python allow an inner function to use the variables of the enclosing function, resolved by lexical (static) scope through a chain of scopes.
- Rule: the innermost declaration hides an outer variable of the same name.
Source: II.9 Global, Local and Shared Variables (2017)
2018 · Paper-I · Q2(d) · 3 marks
Q. List any four best practices for coding standards.
Quick revision — key points
- Give four distinct practices with their purpose: naming, formatting, documentation and modularity.
Worked answer
- Use meaningful, consistent names for variables, functions and classes.
- Format code consistently with proper indentation and spacing.
- Write clear comments and documentation, explaining why and not only what.
- Avoid magic numbers (use named constants) and break work into small, reusable functions.
- Also: handle errors, use version control, and review code.
Source: II.6 Program Development and Execution
2018 · Paper-I · Q3(a) · 5 marks
Q. Write a grade average calculator. Input marks for five subjects, output the average and an appropriate message.
Quick revision — key points
- Read exactly five marks; accumulate; divide by five; use the paper's specified message thresholds.
Worked answer
float sum = 0, m;
for (int i = 1; i <= 5; i++) { cout << "Marks " << i << ": "; cin >> m; sum += m; }
float avg = sum / 5;
cout << "Average = " << avg << endl;
if (avg >= 80) cout << "Well done";
else if (avg >= 60) cout << "Keep it up";
else cout << "Better luck next time";
Source: II.11 Control Structures
2018 · Paper-I · Q3(b) · 2 marks
Q. Write the conditions: (i) print hello when i is twice j, or j is smaller than k and n, or m is negative; (ii) print hello when i is not in the interval 6–9.
Quick revision — key points
- Translate each clause separately; parenthesise AND groups inside OR; outside an interval uses OR.
Worked answer
- (i)
if (i == 2*j || (j < k && j < n) || m < 0) - (ii)
if (i < 6 || i > 9)
Source: II.11 Control Structures
2018 · Paper-I · Q3(c) · 1 mark
Q. Write an equivalent instruction to w += 2*z + 4; without the += operator.
Quick revision — key points
- Expand compound assignment while preserving the original right-hand expression.
Worked answer
w = w + 2 * z + 4;
Source: II.1 Basic Programming Elements and Concepts
2018 · Paper-I · Q3(d) · 2 marks
Q. Predict the values of a and b after every instruction: a=5, b=6; a=(b++)+3; b=--a;
Quick revision — key points
- Post-increment uses the old value; pre-decrement changes the value first; trace each statement.
Worked answer
a = (b++) + 3:b(6) is used first, so a = 9, thenbbecomes 7. → a = 9, b = 7.b = --a:ais decremented first to 8, then assigned. → a = 8, b = 8.
Source: II.1 Basic Programming Elements and Concepts
2018 · Paper-I · Q3(e) · 5 marks
Q. Print a triangle of increasing numbers (for n = 4: 1 2 3 4 / 1 2 3 / 1 2 / 1).
Quick revision — key points
- Rows get shorter: n, n−1, …, 1; each row prints ascending integers from 1.
Worked answer
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) cout << j << " ";
cout << endl;
}
- The outer loop controls the row length and counts down. The inner loop prints 1 up to the row length.
Source: II.11 Control Structures
2018 · Paper-I · Q3(f) · 5 marks
Q. Replace the character at position pos in string st with ch (for "helloworld", position 2 and i gives "hilloworld").
Quick revision — key points
- Check the position; convert one-based position to zero-based index; replace one character.
Worked answer
if (pos >= 1 && pos <= size)
st[pos - 1] = ch; // positions start at 1, array index at 0
cout << st;
Source: II.14 Arrays, Pointers, and Strings
2018 · Paper-I · Q5(a) · 3 marks
Q. What is a dangling pointer?
Quick revision — key points
- Define a pointer to an object whose lifetime has ended; explain causes, undefined behaviour and prevention.
Worked answer
- A dangling pointer points to memory that has already been freed or has gone out of scope.
- Causes: using a pointer after
delete/free, returning the address of a local variable, or a pointer to a block that has ended. - Danger: undefined behaviour, crashes and security holes.
- Fix: set the pointer to
nullptrafter deleting it, and do not return addresses of local variables.
Source: II.14 Arrays, Pointers, and Strings
2019 · Paper-I · Q3(a) · 10 marks
Q. Identify the logical errors in the prime-number program and give the correct statement at the same line number.
Quick revision — key points
- Correct each supplied line; use remainder for divisibility; start at 2; handle n < 2.
Worked answer
- Line 1:
int n, i;(the type is missing). - Line 2:
bool isPrime = true;(variable names cannot contain spaces, and the flag must start as true, because the loop only sets it to false). - Line 5:
for (i = 2; i <= n / 2; ++i)(starting at 1 divides every number by 1 and always gives "not prime"; the limit should include n/2). - Line 7:
if (n % i == 0)(use the remainder operator, not division). - Line 9:
isPrime = false; - Line 13:
if (isPrime && n > 1)(the name had a space, and numbers 1 or below are not prime).
Source: II.11 Control Structures
2019 · Paper-I · Q3(b) · 5 marks
Q. What is the difference between call by value and call by reference?
Quick revision — key points
- Compare copy versus alias; show caller values after a function call; distinguish a reference parameter from a copied pointer.
Worked answer
| Basis | Call by value | Call by reference |
|---|---|---|
| What is passed | A copy of the argument | The address or an alias of the argument |
| Effect on caller's variable | None | Changes are visible to the caller |
| Memory | Extra copy is made | No copy, so efficient for large objects |
| Syntax | void f(int x) | void f(int &x) or void f(int *x) |
void swapV(int a, int b) { int t = a; a = b; b = t; } // caller unchanged
void swapR(int &a, int &b) { int t = a; a = b; b = t; } // caller swapped
Source: II.12 Functions & Parameter Passing
2019 · Paper-I · Q3(c) · 5 marks
Q. What is the role of preprocessor directives? Give three examples in C++.
Quick revision — key points
- Explain preprocessing; give include, macro and conditional-compilation examples.
Worked answer
- Role: preprocessor directives are instructions beginning with
#that are handled before compilation. They include files, define macros and constants, and select which code is compiled. #include: inserts a header file, e.g.#include <iostream>.#define: defines a macro or constant, e.g.#define PI 3.14159.#ifndef / #define / #endif: conditional compilation and include guards, which stop a header from being included twice.- Others:
#undef,#ifdef,#pragma once.
Source: II.8 Program Structure & Data Types
2019 · Paper-I · Q5(b) · 5 marks and 2023 · Paper-I · Q3(c) · 6 marks
Q. What is bit twiddling? Give a brief description.
Quick revision — key points
- Define bitwise operations; demonstrate masks; use unsigned values and valid shift counts.
Worked answer
- Bit twiddling (bit manipulation) means working directly on the individual bits of data using bitwise operators
&,|,^,~,<<,>>. - Why: it is fast and memory-efficient, so it is used in device drivers, graphics, cryptography, compression and flags.
- Examples:
- odd or even:
n & 1 - multiply by 2:
n << 1 - set bit k:
n | (1 << k) - clear bit k:
n & ~(1 << k) - toggle bit k:
n ^ (1 << k) - power of two test:
n != 0 && (n & (n - 1)) == 0 - swap without a temporary:
a ^= b; b ^= a; a ^= b;
- odd or even:
Source: II.15 Structures, Unions, and Bit Manipulation
2020 · Paper-I · Q2(a) · 8 marks
Q. Write a C/C++ program implementing binary logical AND, OR and NOT gates.
Quick revision — key points
- Validate binary inputs; distinguish unary NOT from binary AND/OR; show a truth-table dry run.
Worked answer
int a, b; char g;
cout << "Gate (A=AND, O=OR, N=NOT): "; cin >> g;
if (g == 'N' || g == 'n') { cin >> a; cout << !a; }
else {
cin >> a >> b;
if (g == 'A' || g == 'a') cout << (a && b);
else if (g == 'O' || g == 'o') cout << (a || b);
}
Source: II.11 Control Structures
2020 · Paper-I · Q2(b) · 7 marks
Q. Input a number and print the Fibonacci series up to it.
Quick revision — key points
- Clarify value ceiling versus term count; update two previous terms; handle the input range and integer limits.
Worked answer
int n;
if (!(cin >> n) || n < 0) return 1;
long long a = 0, b = 1; // wide enough for sums while a <= an int ceiling
while (a <= n) {
cout << a << " ";
long long c = a + b; a = b; b = c;
}
- Each term is the sum of the previous two (0, 1, 1, 2, 3, 5 …).
Source: II.11 Control Structures
2020 · Paper-I · Q4(c) · 4 marks
Q. Convert numbers into words (123 → one hundred and twenty three), continuing until the user types quit.
Quick revision — key points
- Use a lookup for small numbers and tens; split hundreds; state the supported range; handle quit and invalid input.
Worked answer
#include <iostream>
#include <string>
using namespace std;
string ones[] = {"","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string tens[] = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
string words(int n) { // 1..999
string s = "";
if (n >= 100) { s += ones[n/100] + " hundred"; n %= 100; if (n) s += " and "; }
if (n >= 20) { s += tens[n/10]; if (n % 10) s += " " + ones[n % 10]; }
else s += ones[n];
return s;
}
int main() {
string in;
while (true) {
cout << "Enter a number (or quit): ";
if (!(cin >> in)) break;
if (in == "quit") break;
try {
size_t used = 0;
int n = stoi(in, &used);
if (used != in.size() || n < 0 || n > 999) {
cout << "Enter an integer from 0 to 999." << endl;
continue;
}
cout << (n == 0 ? "zero" : words(n)) << endl;
} catch (...) {
cout << "Enter an integer from 0 to 999, or quit." << endl;
}
}
}
Source: II.11 Control Structures
Scope: This existing implementation covers 0–999; reject values outside that range and validate numeric input before conversion. It is not a solution for arbitrary integers.
2021 · Paper-I · Q3(b) · 5 marks
Q. Write a program that outputs the factors of a number, one per line.
Quick revision — key points
- For positive n, test divisors from 1 through n; print each i with n % i == 0.
Worked answer
int x; cin >> x;
for (int i = 1; i <= x; i++)
if (x % i == 0) cout << i << endl;
Source: II.11 Control Structures
2021 · Paper-I · Q3(c) · 5 marks
Q. What are IDEs? How do they help in software development? List the IDEs you have used.
Quick revision — key points
- Define an IDE; connect features to development tasks; list only tools you have used when asked.
Worked answer
- An IDE (Integrated Development Environment) is one program that combines a code editor, compiler or interpreter, debugger and build tools.
- How it helps: syntax highlighting, auto-completion, instant error checking, one-click build and run, debugging with breakpoints, project management and version-control integration. All this saves time and reduces mistakes.
- Common IDEs: Visual Studio, Visual Studio Code, Code::Blocks, Dev-C++, Eclipse, NetBeans, IntelliJ IDEA, PyCharm, Android Studio. (In the exam, list the ones you have actually used.)
Source: II.6 Program Development and Execution
2021 · Paper-I · Q3(d) · 4 marks
Q. Write a C/C++ program to convert a decimal number to hexadecimal.
Quick revision — key points
- Repeatedly divide by 16; map remainders 10–15 to A–F; reverse the digits; handle zero.
Worked answer
int n; cin >> n;
char hex[32]; int i = 0;
if (n == 0) cout << 0;
while (n > 0) {
int r = n % 16;
hex[i++] = (r < 10) ? '0' + r : 'A' + (r - 10);
n /= 16;
}
for (int j = i - 1; j >= 0; j--) cout << hex[j];
- Remainders are read in reverse order.
Source: II.11 Control Structures
2021 · Paper-I · Q4(c) · 6 marks
Q. What do you mean by runtime and compile-time errors?
Quick revision — key points
- Compare detection stage and examples; distinguish compile, link, runtime and logical errors.
Worked answer
| Basis | Compile-time error | Runtime error |
|---|---|---|
| When found | While compiling, before the program runs | While the program is running |
| Cause | Syntax mistakes, undeclared variables, type mismatch | Divide by zero, invalid memory access, file not found, array out of range |
| Result | Translation fails; no new successful build | May be handled, throw an exception, crash, or cause undefined behaviour; failure is not guaranteed to be visible |
| Example | Missing semicolon | x = 10 / 0; |
- Logical errors are different again: the program runs but gives wrong results.
Source: II.6 Program Development and Execution
2021 · Paper-I · Q5(b) · 6 marks
Q. What is pass by value and pass by reference? Can we pass an object as a parameter to a method in Java?
Quick revision — key points
- Java copies the reference value: object mutation is visible, parameter reassignment does not replace the caller's reference.
Worked answer
- Pass by value: a copy of the argument is passed, so changes inside the method do not affect the original.
- Pass by reference: the method works on the original variable (in C++ through
&or pointers). - Java: yes, an object can be passed as a parameter. Java passes the reference to the object by value, so the method can change the object's fields and the caller sees the change. Reassigning the parameter to a new object does not affect the caller's reference.
Source: II.12 Functions & Parameter Passing
2022 · Paper-I · Q2(a) · 8 marks
Q. Write a function pattern(int n) that prints the right-aligned number triangle (1 / 1 2 / 1 2 3 …).
Quick revision — key points
- Use a void function; separate spacing and number loops; match the shown alignment.
Worked answer
void pattern(int n) { // return type void
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) cout << " "; // leading spaces
for (int j = 1; j <= i; j++) cout << j << " ";
cout << endl;
}
}
- Each missing number is replaced by two spaces, for single-digit entries. For rows containing numbers above 9, use a consistent field width (for example
setw) for both spaces and values.
Source: II.11 Control Structures
2022 · Paper-I · Q2(c) · 6 marks
Q. Write the output:
int ary[4]={1,2,3,4}; int *p1=ary+3; cout<<p1[-2]<<endl; int *p2=&p1[-2]; *p2=10; cout<<ary[1]<<endl;
Quick revision — key points
- Draw the array and pointer positions; negative offset is valid only when the resulting element stays inside the array.
Worked answer
p1points toary[3], sop1[-2]isary[1]= 2.p2points toary[1], and*p2 = 10changes it.- Output:
2then10.
Source: II.14 Arrays, Pointers, and Strings
2022 · Paper-I · Q3(c) · 6 marks
Q. Write the output:
int v1=55; int* p1=&v1; int* p2; p2=p1; *p1=20; cout<<*p2<<endl; cout<<v1<<endl;
Quick revision — key points
- Both pointers alias one object; updating through either pointer changes what both observe.
Worked answer
p1andp2both point tov1. Setting*p1 = 20changesv1.- Output:
20then20.
Source: II.14 Arrays, Pointers, and Strings
2022 · Paper-I · Q4(a) · 12 marks
Q. Write a C++ program to check whether a string is a palindrome without string library functions, taking input with cin.getline.
Quick revision — key points
- Read the line; find its length manually; compare mirrored characters; stop at the first mismatch.
Worked answer
#include <iostream>
using namespace std;
int main() {
char s[100];
cout << "Enter a string: ";
cin.getline(s, 100);
int len = 0;
while (s[len] != '\0') len++; // find length manually
bool pal = true;
for (int i = 0; i < len / 2; i++)
if (s[i] != s[len - 1 - i]) { pal = false; break; }
cout << (pal ? "Palindrome" : "Not a palindrome");
return 0;
}
- It compares the first character with the last, the second with the second last, and so on up to the middle.
Source: II.14 Arrays, Pointers, and Strings
2022 · Paper-I · Q4(b) · 8 marks
Q. Write the output of the program with a = 12, b = 25 that prints a | b.
Quick revision — key points
- Write both numbers in binary; OR each bit; convert 11101 back to 29.
Worked answer
- 12 = 01100, 25 = 11001, so 12 | 25 = 11101 = 29.
- Output:
a = 12
b = 25
a | b = 29
Source: II.15 Structures, Unions, and Bit Manipulation
2022 · Paper-I · Q6(a) · 8 marks
Q. Complete the recursive function int power(int k, int n) to calculate kⁿ without library functions.
Quick revision — key points
- State n ≥ 0; base case power(k,0)=1; reduce n at each call; discuss overflow limits.
Worked answer
int power(int k, int n) {
if (n == 0) return 1; // base case
return k * power(k, n - 1); // recursive case
}
- Optional faster version: for even n, compute
p = power(k, n/2)and returnp * p(time O(log n)).
Source: II.12 Functions & Parameter Passing
2023 · Paper-I · Q3(a) · 8 marks
Q. How is dynamic memory allocation managed in a C++ program? Clarify with a program.
Quick revision — key points
- Allocate the requested size; use the array; release with delete[]; prevent leaks and dangling aliases.
Worked answer
newallocates memory on the heap at run time and returns its address.delete(ordelete[]for arrays) releases it. In C the equivalents aremalloc/free.- Why: the size is not known at compile time, and the memory lives until it is freed.
- Risks: memory leaks (forgetting to free), dangling pointers and double deletion.
int n; cin >> n;
int *arr = new int[n]; // allocate
int sum = 0;
for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; }
cout << "Sum = " << sum;
delete[] arr; // release
arr = nullptr;
Source: II.14 Arrays, Pointers, and Strings
2024 · Paper-I · Q3(a) · 6 marks
Q. Prompt for a letter grade A, B, C, D or F and display 4, 3, 2, 1 or 0.
Quick revision — key points
- Map each grade to a number; include a default case; handle letter case deliberately.
Worked answer
char g; cin >> g;
switch (toupper(g)) {
case 'A': cout << 4; break;
case 'B': cout << 3; break;
case 'C': cout << 2; break;
case 'D': cout << 1; break;
case 'F': cout << 0; break;
default: cout << "Invalid grade";
}
Source: II.11 Control Structures
2024 · Paper-I · Q3(b) · 6 marks
Q. Write pseudocode or C for the dot product a·b = Σ aᵢbᵢ.
Quick revision — key points
- Initialise the accumulator to zero; multiply corresponding components; sum exactly n terms.
Worked answer
sum = 0
for i = 1 to n
sum = sum + a[i] * b[i]
print sum
Source: II.11 Control Structures
2024 · Paper-I · Q4(a) · 6 marks
Q. Display the area and perimeter of a rectangle with width 4.5 and height 7.9.
Quick revision — key points
- Area = width × height; perimeter = 2(width + height); state units where given.
Worked answer
float w = 4.5, h = 7.9;
cout << "Area = " << w * h << endl; // 35.55
cout << "Perimeter = " << 2 * (w + h) << endl; // 24.8
Source: II.11 Control Structures
2024 · Paper-I · Q4(b) · 6 marks
Q. Read a Celsius degree and display the Fahrenheit value using F = (9/5) × C + 32.
Quick revision — key points
- Use floating-point 9.0/5.0; substitute Celsius; add 32; check a known conversion.
Worked answer
float c; cin >> c;
cout << "Fahrenheit = " << (9.0 / 5) * c + 32;
- Use
9.0 / 5. In C/C++ the integer expression9 / 5equals 1 and gives a wrong answer.
Source: II.1 Basic Programming Elements and Concepts
2024 · Paper-I · Q4(c) · 8 marks
Q. Enter a month and year and display the number of days in that month.
Quick revision — key points
- Validate month; apply the 400/100/4 leap-year rule; distinguish 28/29/30/31 days.
Worked answer
int m, y, d;
if (!(cin >> m >> y) || m < 1 || m > 12 || y < 1) {
cout << "Invalid month or year";
return 1;
}
bool leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
switch (m) {
case 2: d = leap ? 29 : 28; break;
case 4: case 6: case 9: case 11: d = 30; break;
default: d = 31;
}
cout << "The month has " << d << " days";
- Example: month 2, year 2024 → 29 days. Month 3, year 2015 → 31 days.
Source: II.11 Control Structures
Input guard: Check 1 ≤ month ≤ 12 before the switch; otherwise its default branch incorrectly treats an invalid month as a 31-day month.
2024 · Paper-I · Q5(a) · 6 marks
Q. Show the output of the Java code: Double x = 3.5; System.out.println(x.intValue()); System.out.println(x.compareTo(4.5));
Quick revision — key points
- intValue truncates 3.5 to 3; compareTo reports that 3.5 is less than 4.5.
Worked answer
x.intValue()converts the Double to an int by dropping the fractional part → 3.x.compareTo(4.5)returns a negative number whenxis less than the argument → -1.- Output:
3
-1
Source: II.13 Standard Library Functions
2025 · Paper-I · Q4(a) · 6 marks
Q. Output the arithmetic series of n terms and its sum Sₙ = (n/2)[2a + (n−1)d], with a = 5, d = 3, n = 6.
Quick revision — key points
- Generate a+i×d for i=0…n−1; accumulate or use the formula; expected series sums to 75.
Worked answer
int a = 5, d = 3, n = 6;
for (int i = 0; i < n; i++) cout << a + i * d << " "; // 5 8 11 14 17 20
cout << "\nSum = " << n * (2 * a + (n - 1) * d) / 2; // 75
Source: II.11 Control Structures
2025 · Paper-I · Q4(b) · 6 marks
Q. Write a C++ program to calculate and display the product of two 2 × 2 matrices.
Quick revision — key points
- Initialise the result matrix; use row, column and inner-product loops; print all four results.
Worked answer
int A[2][2], B[2][2], C[2][2] = {{0,0},{0,0}};
for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) cin >> A[i][j];
for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) cin >> B[i][j];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
C[i][j] += A[i][k] * B[k][j];
for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) cout << C[i][j] << " "; cout << endl; }
Source: II.1 Basic Programming Elements and Concepts
2025 · Paper-I · Q4(c) · 8 marks
Q. Demonstrate two complex numbers using a structure. Use a function to add them and display the result as (a + bi).
Quick revision — key points
- Represent real and imaginary parts; add corresponding fields; format negative imaginary parts correctly.
Worked answer
struct Complex { float re, im; };
Complex add(Complex x, Complex y) { return { x.re + y.re, x.im + y.im }; }
void show(Complex c) {
cout << "(" << c.re << (c.im < 0 ? " - " : " + ") << (c.im < 0 ? -c.im : c.im) << "i)";
}
int main() {
Complex a, b;
cout << "First (real imag): "; cin >> a.re >> a.im;
cout << "Second (real imag): "; cin >> b.re >> b.im;
show(add(a, b));
}
Source: II.15 Structures, Unions, and Bit Manipulation
2026 · Paper-I · Q3(a) · 6 marks
Q. What is bit manipulation in programming, and why is it used? Give two examples.
Quick revision — key points
- Explain bit manipulation; show checking and setting/clearing bits; avoid blanket performance claims.
Worked answer
- Bit manipulation uses bitwise operators (
&,|,^,~,<<,>>) to read or change individual bits. - Why used: speed, low memory use, and direct control of flags, masks, hardware registers, compression and encryption.
- Example 1: checking odd or even with
n & 1(1 means odd). - Example 2: setting and clearing a bit with
flags |= (1 << 3)andflags &= ~(1 << 3).
Source: II.15 Structures, Unions, and Bit Manipulation
2026 · Paper-I · Q3(b) · 6 marks
Q. What are arrays? Describe one advantage and one limitation.
Quick revision — key points
- Define contiguous same-type storage; explain indexed access; describe fixed extent and shifting costs.
Worked answer
- An array is a fixed-size collection of elements of the same type stored in contiguous memory and accessed by index.
- Advantage: direct (random) access to any element in O(1) using its index, plus good cache performance.
- Limitation: its size is fixed when it is created. Insertion or deletion in the middle needs shifting elements (O(n)), and the memory cannot grow without making a new array.
Source: II.14 Arrays, Pointers, and Strings
2026 · Paper-I · Q3(c) · 8 marks
Q. Write pseudocode to reverse an array using pointers.
Quick revision — key points
- Return immediately for fewer than two elements; initialise pointers only then; swap and move inward.
Worked answer
if n < 2: return
left = &arr[0]
right = &arr[n - 1]
while left < right
temp = *left
*left = *right
*right = temp
left = left + 1
right = right - 1
- Time O(n), extra space O(1).
Source: II.14 Arrays, Pointers, and Strings
3. MCQs and explanations
Topic II.6
2024 P-I · Q13. When compiled, which of the following classes of statement often yields no executable code?
- (A) Assignment statement
- (B) Structural statements ✅
- (C) Input and output statements
- (D) None of these
- Answer: (B) Structural statements.
- Why: declarations and other structural statements only tell the compiler about the program's structure (types, names, blocks), so they usually produce no machine instructions. Assignments and input/output statements are translated into executable instructions.
2024 P-I · Q15. A compiler can check errors.
- (A) Syntax ✅
- (B) Content
- (C) Logical
- (D) None of these
- Answer: (A) Syntax.
- Why: a compiler can detect violations of the language grammar (a missing semicolon or bracket) and type errors, but it cannot know what the programmer meant, so logical errors (wrong result) are not found at compile time.
Source: II.6 Program Development and Execution
Topic II.8
2025 P-I · Q6. Which of the following is the false statement in the C language?
- (A)
static a = 10; - (B)
static int func (int); - (C)
static static int a;✅ - (D) None of these
- Answer: (C)
static static int a;. - Why: a declaration may have only one storage-class specifier, so repeating
staticis a compile error. (A) is accepted by older C compilers (the type defaults to int), and (B) is a valid declaration of a function with internal linkage.
Source: II.8 Program Structure & Data Types
Topic II.12
2025 P-I · Q15. What is the output of the following code? int a = 40; int* ptr = &a; int& ref = *ptr; ref = 60; cout << a;
- (A) 40
- (B) 60 ✅
- (C) Error
- (D) None of these
- Answer: (B) 60.
- Step by step:
ptr = &a, so*ptris the variableaitself.int& ref = *ptrmakesrefanother name (alias) fora.ref = 60therefore stores 60 ina, andcout << aprints 60.
- Why not the others: the code is valid, so there is no error, and
ais changed through the alias, so it is not 40.
2026 P-I · Q7. A function needs to modify an input variable. How should the parameter be passed?
- (A) By value
- (B) By reference ✅
- (C) By pointer
- (D) None of these
- Answer: (B) By reference.
- Why: pass by value gives the function only a copy, so the caller's variable cannot change. Passing by reference lets the function work on the original variable. (A pointer would also work, but "by reference" is the standard C++ answer and the cleaner syntax.)
Source: II.12 Functions & Parameter Passing
Topic II.14
2024 P-I · Q16. Select the correct output of the following code: int arr[5] = {10,20,30,40,50}; printf("%d", arr[5]);
- (A) Garbage value ✅
- (B) 20
- (C) 30
- (D) None of these
- Answer: (A) Garbage value.
- Correct explanation: valid indices are 0–4. Reading arr[5] is undefined behaviour, with no guaranteed output. The stored option “garbage value” is only the source's exam shorthand.
Source: II.14 Arrays, Pointers, and Strings
Topic II.15
2026 P-I · Q3. A programmer wants to group different data types. What should he or she use?
- (A) Structures ✅
- (B) Arrays
- (C) Unions
- (D) None of these
- Answer: (A) Structures.
- Why: a structure holds members of different types together, each with its own memory (for example an int, a float and a char array). An array holds elements of one type only. A union also mixes types, but all members share the same memory, so only one value is valid at a time.
Source: II.15 Structures, Unions, and Bit Manipulation
4. Source discrepancies and technical checks
- The older Most Repeated page labels prime debugging, value/reference and preprocessing as 2020 Q3(a–c); the detailed topic records label them 2019 Q3(a–c). Keep this discrepancy visible until the original paper is checked.
- The older page labels palindrome as 2019 Q4(a); the detailed record is 2022 Q4(a), 12 marks, with no string-library functions. The grade mapping is 2024 Q3(a) in the detailed record, not Q2(a).
- The 2018 decreasing triangle and 2022 right-aligned increasing triangle are different tasks; do not memorise one shape for both.
- The 2024 array MCQ's stored answer “garbage value” is only an exam shorthand. Out-of-bounds access has undefined behaviour; there is no guaranteed output. Pointer arithmetic must stay within the permitted array bounds. C++ working draft: pointer arithmetic.
- For the 2025 static-declaration MCQ, the implicit-int option depends on obsolete C rules; do not teach it as valid modern C or C++.
- Union storage is sufficient for its largest member, with alignment requirements; only one non-static member is active at a time, subject to specific language exceptions. Do not assume arbitrary inactive-member type-punning is portable C++. C++ working draft: unions.
- Bitwise operations are useful for masks and representations; they are not automatically faster than arithmetic after optimisation. Use unsigned masks and shifts within the type's width.
5. Practice checklist
- Attempt all indexed written tasks using the recorded marks as a guide.
- Trace every output question before reading its answer.
- Reproduce each program with valid, boundary and invalid inputs.
- Review all seven MCQ records and note ambiguous wording.
- Resolve disputed references against original papers before quoting them as verified.