Fundamentals of Programming
Fundamentals of Programming
Related Question:
Q1. In which step of the problem-solving life cycle are the input data, procedure, and expected
output identified?
1.4 Testing
Verify the program's correctness and ensure it meets all requirements by identifying and fixing
errors (bugs).
Related Question:
Q26. Which phase of the problem-solving life cycle verifies correctness and ensures
requirements are met?
A. Testing
B. Debugging
C. Analysis
D. Maintenance
Answer: A. Testing
2. Algorithms
An algorithm is a finite set of well-defined instructions to accomplish a task. A good algorithm
has specific properties, such as finiteness.
Related Question:
Answer: D. A and C
3. Programming Approaches
Different approaches guide software design and development.
Related Questions:
A. Left-right
B. Bottom-up
C. Right-left
D. Top-down
Answer: D. Top-down (C++ supports top-down design through functions and object-oriented
design).
A. Left-right
B. Right-left
C. Bottom-up
D. Top-down
4. C++ Fundamentals
C++ is a powerful, general-purpose programming language with robust features.
4.1 Variable Declaration and Naming
Variables must be declared with a data type, and naming follows specific rules.
Related Questions:
A. _something
B. aVariable
C. float2string
D. 2manyLetters
E. X
A. VAR_1234
B. varname
C. 7VARNAME
D. 7varname
Answer: A. VAR_1234 (Identifiers cannot start with a number or contain special characters like
' ).
Related Questions:
A. int
B. char
C. float
D. double
Answer: D. double (It typically has the largest range for floating-point numbers).
A. int a;
B. float b;
C. double d;
D. int c = 40;
Answer: D. int c = 40; (This includes initialization, unlike the others which are declarations
only).
4.3 Operators
Operators perform operations on variables and values.
Related Questions:
Answer: A. Pre-increment is usually faster than post-increment (It avoids creating a temporary
copy).
#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << --x + 1 << ",";
cout << x++;
return 0;
}
Explanation:
x = 10
--x : x becomes 9, expression evaluates to 9.
--x + 1 : 9 + 1 = 10 , prints 10 .
x++ : Current x (9) is printed, then x becomes 10.
Answer: A. 10,9
#include <iostream>
using namespace std;
int main() {
int k = 2, g = 20;
k *= g++;
cout << k << " , " << g;
return 0;
}
Explanation:
k = 2 , g = 20
k *= g++ : Equivalent to k = k * (g++) .
g++ uses g ’s current value (20), then increments g to 21.
k = 2 * 20 = 40 .
Print k (40) and g (21).
Answer: B. 40 , 21
int var;
var = 3 + 2 * 3 / 2 - (4 - 2);
Explanation (PEMDAS/BODMAS):
1. Parentheses: (4 - 2) = 2
2. Multiplication/Division (left to right):
2 * 3 = 6
6 / 2 = 3
3. Addition/Subtraction: 3 + 3 - 2 = 4
Answer: C. 4
#include <iostream>
using namespace std;
int main() {
int a, b, c;
a = 2;
b = 7;
c = (a > b) ? a : b;
cout << c;
return 0;
}
Explanation:
a = 2, b = 7
c = (a > b) ? a : b : (2 > 7) is false, so c = b = 7 .
Prints 7 .
Answer: D. 7
int a = 4, b = 9, c;
c = a++ + ++b;
Explanation:
a = 4, b = 9
a++ : Uses 4 , then a becomes 5.
++b : b becomes 10, uses 10.
c = 4 + 10 = 14 .
Answer: C. 14
4.4 Arrays
Arrays store similar data items in contiguous memory locations.
Related Questions:
Answer: A. A set of similar data items (Arrays store elements of the same type).
A. Index of an array
B. Elements of the array
C. Functions of the array
D. All of the above
A. Compile time
B. Run time
C. Not an error
D. None
Answer: B. Run time (Accessing out-of-bounds indices leads to undefined behavior, typically a
runtime error).
A. array{10};
B. array array[10];
C. int array;
D. int array[10];
A. Heterogeneous datatype
B. Homogeneous datatype
C. Store continuous data
D. Access elements through index
Q12. Which is the correct syntax for declaring an array of pointers to integers with size 10 in
C++?
Explanation:
Answer: D. int *arr = new int[10]; (Note: The question asks for an array of pointers, but
the options suggest an array of integers. Assuming a typo, the correct answer is for an array of
integers).
#include <iostream>
using namespace std;
int main() {
int i = {1, 2, 3};
cout << i << endl;
return 0;
}
Explanation: Initializing an int with {1, 2, 3} is invalid; it’s meant for arrays or aggregate
types. This causes a compile-time error.
Answer: D. Error
Explanation: In Java, == compares array references, not contents. Since a and b are
distinct objects, the output is false .
Q38. Which instruction implements decision-making for doing one thing or another?
A. while
B. for
C. if..else
D. sequence
E. cin
Answer: C. if..else
#include <iostream>
using namespace std;
int main() {
float mark = 50.0L;
if (mark > 85)
cout << "Your grade is A ";
cout << "Excellent!";
return 0;
}
Explanation:
mark = 50.0
if (mark > 85) is false, so cout << "Your grade is A "; is skipped.
cout << "Excellent!"; is outside the if block and always executes.
Answer: C. Excellent!
#include <iostream>
using namespace std;
int main() {
if (6 > 8) {
cout << "**" << endl;
cout << "****" << endl;
}
else if (9 == 4)
cout << "***" << endl;
else
cout << "*" << endl;
return 0;
}
Explanation:
6 > 8 is false.
9 == 4 is false.
The else block executes, printing * .
Answer: C. *
A. Do While Loop
B. For Loop
C. While Loop
D. Do Loop
i. Condition is checked
ii. Initialization is executed
iii. Increment/Decrement
iv. Statement is executed
Answer: A. ii, i, iv, iii (Initialization, condition check, body execution, increment/decrement).
#include <iostream>
using namespace std;
int main() {
int a = 4, b = 12;
do {
cout << "in the loop" << endl;
a += 2;
b -= 2;
} while (a < b);
return 0;
}
Explanation:
Q31. How many times does sum get incremented in the following code?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum++;
}
}
cout << sum;
return 0;
}
Explanation:
Answer: C. 9
Q27. A statement that skips the remaining statements in a loop and proceeds to the next
iteration is:
A. Break statement
B. Continue statement
C. Case statement
D. None of them
4.6 Functions
Functions are reusable code blocks for specific tasks.
Related Questions:
Q42. Which is not part of every C++ function?
A. Function header
B. Function body
C. Function selector
D. Function parameters
E. All except C
A. Function invocation
B. Function prototyping
C. Not a valid C++ statement
D. Variable declaration
A. Call by object
B. Call by pointer
C. Call by value
D. Call by reference
Answer: D. Call by reference (Avoids copying large objects, safer than pointers).
A. calculateArea(area, radius);
B. area = calculateArea(radius);
C. calculateArea(&area, radius);
D. calculateArea(area);
Related Questions:
#include <iostream>
using namespace std;
int main() {
register int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}
Explanation: Taking the address of a register variable is not allowed, causing a compile-
time error.
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10, c = 15;
int arr[3] = {&a, &b, &c}; // Incorrect: int* arr[3] needed
cout << *arr[*arr[1] - 8];
return 0;
}
Explanation: int arr[3] = {&a, &b, &c}; causes a compile-time error due to type
mismatch. If corrected to int* arr[3] , the expression *arr[*arr[1] - 8] evaluates to 15 .
Related Questions:
A. Inheritance
B. Polymorphism
C. Abstraction
D. Encapsulation
Answer: A. Inheritance
A. Inheritance
B. Polymorphism
C. Abstraction
D. Encapsulation
Related Questions:
D. Both A and C
Answer: D. Both A and C (An object is an instance of a class, defining its data type).
Answer: B. Member functions are public, member variables are private (Encapsulation
principle).
A. Member data
B. Functions
C. Both member data & functions
D. Protected members
A. Protected
B. Public
C. Private
D. Either A or C
5.1.5 Inheritance
Related Questions:
5.1.6 Polymorphism
Related Question:
Q86. Which feature allows objects with different internal structures to share the same external
interface?
A. Objects
B. Classes
C. Polymorphism
D. Message passing
Answer: C. Polymorphism
A. Any class
B. Class from which any class is derived
C. Class with at least one virtual function
D. Class with at least one pure virtual function
Related Questions:
5.2 Constructors
Related Question:
class Example {
public:
int a, b, c;
Example() { a = b = c = 1; } // Constructor 1
Example(int a) { a = a; b = c = 1; } // Constructor 2
Example(int a, int b) { a = a; b = b; c = 1; } // Constructor 3
Example(int a, int b, int c) { a = a; b = b; c = c; } // Constructor 4
};
Explanation: Matches Constructor 4, but a = a does not assign to the member variable (use
this->a = a ).
Answer: B. Constructor 4
6. Input/Output (I/O)
C++ uses streams for I/O operations.
Related Questions:
7. Exception Handling
Exception handling manages errors gracefully.
Related Questions:
Q14. What happens when the try block is moved far away from the catch block?
Explanation: Syntactically, catch must follow try , or it’s a compile-time error. If logically
separated (e.g., in different functions), exception handling may increase code size due to stack
unwinding.
A. catch
B. throw
C. try
D. finally
Answer: C. try
I. Linking
II. Compiling
III. Execution
IV. Processing #include directives
Answer: D. All
A. Dynamic Loading
B. Dynamic binding
C. Data hiding
D. Both A & B
11.2 Modularity
Related Question:
A. Addressof
B. Unary operator
C. Logical not
D. Array element access
#include <iostream>
using namespace std;
int main() {
int i = 0;
label:
cout << "Interviewbit";
i++;
if (i < 3) {
goto label;
}
return 0;
}
Answer: C. 3 times
A. Local variable
B. Global variable
C. Normal variable
D. Variable scope
#include <iostream>
using namespace std;
int main() {
int x = 4, y = 3; // line 1
const int i = 10; // line 2
x = x + i; // line 3
i = y * 2; // line 4
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {"Steve jobs"};
unsigned long int found = str.find_first_of("aeiou");
while (found != string::npos) {
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
cout << str << "\n";
return 0;
}
#include <iostream>
using namespace std;
int array1[] = {1200, 200, 2300, 1230, 1543};
int array2[] = {12, 14, 16, 18, 20};
int temp, result = 0;
int main() {
for (temp = 0; temp < 5; temp++) {
result += array1[temp];
}
for (temp = 0; temp < 4; temp++) {
result += array2[temp];
}
cout << result;
return 0;
}
Answer: B. 6533
// Program 1
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr1;
arr1.fill(5);
cout << get<5>(arr1);
return 0;
}
// Program 2
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr1;
arr1.fill(5);
cout << arr1.at(5);
return 0;
}
Explanation:
#include <iostream>
using namespace std;
int main() {
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
}
Answer: D. 16
#include <iostream>
using namespace std;
int main() {
char c = 74;
cout << c;
return 0;
}
Answer: B. J