Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Which feature is supported by C++ but not directly by C?
Not quite. Try another option.
Answer: Classes and objects
C++ supports object-oriented programming through classes and objects.
Question 2 of 60 · Data types · Easy
Which data type is commonly used to store a single character in C and C++?
Not quite. Try another option.
Answer: char
The char data type stores a single character.
Question 3 of 60 · Variables · Easy
What is a variable in a program?
Not quite. Try another option.
Answer: A named storage location
A variable is a named memory location whose value can be accessed and changed.
Question 4 of 60 · Constants · Easy
Which C++ keyword can be used to declare a constant?
Not quite. Try another option.
Answer: const
The const keyword prevents a declared value from being modified.
Question 5 of 60 · Reading and writing data using scanf, printf, cin and cout · Easy
Which object is commonly used to display output in C++?
Not quite. Try another option.
Answer: cout
The cout object sends formatted output to the standard output stream.
Question 6 of 60 · Control structures: if-else and loops · Easy
Which control structure repeatedly executes code while a condition remains true?
Not quite. Try another option.
Answer: while loop
A while loop repeats its body as long as its condition evaluates to true.
Question 7 of 60 · Functions: declaration and definition · Easy
What does a function declaration tell the compiler?
Not quite. Try another option.
Answer: The function's name and signature
A function declaration introduces its name, return type, and parameters to the compiler.
Question 8 of 60 · Call by value and call by reference · Easy
What is passed to a function during call by value?
Not quite. Try another option.
Answer: A copy of the argument
Call by value gives the function a copy, so changes do not affect the original argument.
Question 9 of 60 · Recursion · Easy
What is recursion?
Not quite. Try another option.
Answer: A function calling itself
Recursion occurs when a function calls itself to solve a smaller version of a problem.
Question 10 of 60 · Introduction to object-oriented programming · Easy
Which concept combines data and related functions within one unit?
Not quite. Try another option.
Answer: Encapsulation
Encapsulation groups data and the functions that operate on it into a single unit.
Question 11 of 60 · User-defined data types: struct and class · Easy
Which keyword defines a structure in C++?
Not quite. Try another option.
Answer: struct
The struct keyword defines a user-defined type containing related members.
Question 12 of 60 · Classes and objects · Easy
What is an object in C++?
Not quite. Try another option.
Answer: An instance of a class
An object is a specific instance created from a class definition.
Question 13 of 60 · Manager functions: constructors and destructors · Easy
When is a constructor normally called in C++?
Not quite. Try another option.
Answer: When an object is created
A constructor is automatically called when an object of its class is created.
Question 14 of 60 · Introduction to data structures · Easy
What is the main purpose of a data structure?
Not quite. Try another option.
Answer: To organize and store data
A data structure organizes data so it can be accessed and modified effectively.
Question 15 of 60 · Types of data structures · Easy
Which of the following is a linear data structure?
Not quite. Try another option.
Answer: Array
An array stores elements in a linear sequence.
Question 16 of 60 · Time and space complexity analysis · Easy
What does time complexity describe?
Not quite. Try another option.
Answer: Growth in running time
Time complexity describes how an algorithm's running time grows as its input size increases.
Question 17 of 60 · Time-space trade-off · Easy
What does a time-space trade-off mean?
Not quite. Try another option.
Answer: Using more memory to save time
A time-space trade-off often improves execution speed by using additional memory, or saves memory by taking more time.
Question 18 of 60 · Big-O notation · Easy
What does Big-O notation commonly express?
Not quite. Try another option.
Answer: An asymptotic upper bound
Big-O notation expresses an asymptotic upper bound on an algorithm's growth rate.
Question 19 of 60 · Big-Omega notation · Easy
What does Big-Omega notation commonly express?
Not quite. Try another option.
Answer: An asymptotic lower bound
Big-Omega notation expresses an asymptotic lower bound on an algorithm's growth rate.
Question 20 of 60 · Big-Theta notation · Easy
What does indicate about an algorithm's growth?
Not quite. Try another option.
Answer: It grows linearly
gives a tight linear bound on the algorithm's growth rate.
Question 21 of 60 · Introduction to C/C++ · Medium
A C++ program defines int process(int) and double process(double). Which feature allows process(4) and process(4.5) to select different functions?
Not quite. Try another option.
Answer: Function overloading
C++ function overloading allows functions with the same name but different parameter types. The compiler selects the matching version from the arguments.
Question 22 of 60 · Data types · Medium
What does cout << fixed << setprecision(1) << 7 / 2; display when the required headers are included?
Not quite. Try another option.
Answer: 3.0
Both operands are integers, so 7 / 2 evaluates to 3. It is then formatted as 3.0.
Question 23 of 60 · Variables · Medium
What is printed by int x = 5; { int x = 8; cout << x << " "; } cout << x;?
Not quite. Try another option.
Answer: 8 5
The inner declaration shadows the outer variable only inside the block. After the block, the outer x remains 5.
Question 24 of 60 · Constants · Medium
Given int a = 1, b = 2; const int* p = &a;, which statement is valid?
Not quite. Try another option.
Answer: p = &b;
p may point to another integer, but the integer accessed through p cannot be modified because it is viewed as constant.
Question 25 of 60 · Reading and writing data using scanf, printf, cin and cout · Medium
Which pair correctly reads an int n and a double x using C input and then using C++ input?
Not quite. Try another option.
Answer: scanf("%d %lf", &n, &x); and cin >> n >> x;
scanf requires addresses and uses %d for int and %lf for double. C++ input uses the extraction operator >> with cin.
Question 26 of 60 · Control structures: if-else and loops · Medium
What is the final value of sum? int sum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) continue; sum += i; if (sum > 5) break; }
Not quite. Try another option.
Answer: 9
Only odd values are added: 1, 3, and 5. The sum becomes 9, so the loop terminates.
Question 27 of 60 · Functions: declaration and definition · Medium
Given the declaration int combine(int, int = 2); and definition int combine(int a, int b) { return a + b; }, what does combine(5) return?
Not quite. Try another option.
Answer: 7
The declaration supplies 2 as the default second argument, so the call evaluates 5 + 2.
Question 28 of 60 · Call by value and call by reference · Medium
What is printed by void update(int a, int& b) { a += 2; b += 2; } int x = 3, y = 3; update(x, y); cout << x << " " << y;?
Not quite. Try another option.
Answer: 3 5
a is a copy, so changing it does not affect x. The reference b refers to y, so y becomes 5.
Question 29 of 60 · Recursion · Medium
How many total function invocations occur for f(4), including the initial call, if f(n) returns immediately when and otherwise calls f(n-1) and f(n-2)?
Not quite. Try another option.
Answer: 9
The call counts satisfy with , giving .
Question 30 of 60 · Introduction to object-oriented programming · Medium
If Base declares virtual void show() and Derived overrides it, which implementation runs for Base* p = new Derived; p->show();?
Not quite. Try another option.
Answer: Derived::show()
A virtual function is dynamically dispatched according to the object's runtime type, which is Derived.
Question 31 of 60 · User-defined data types: struct and class · Medium
Consider struct S { int x; }; and class C { int x; };. Without adding access specifiers, which statement is correct?
Not quite. Try another option.
Answer: S::x is public and C::x is private
Members of a struct are public by default, whereas members of a class are private by default.
Question 32 of 60 · Classes and objects · Medium
A Counter class has a non-static member value initialized to 0, an increment() method, and a get() method. If object a is incremented twice and object b once, what do a.get() and b.get() return?
Not quite. Try another option.
Answer: 2 and 1
Each object has its own copy of the non-static member value, so changes to one object do not affect the other.
Question 33 of 60 · Manager functions: constructors and destructors · Medium
A Derived object contains a member object Member and inherits from Base. Which order is used for construction and destruction?
Not quite. Try another option.
Answer: Construct Base, Member, Derived; destroy Derived, Member, Base
The base is constructed first, followed by members and then the derived object. Destruction occurs in the reverse order.
Question 34 of 60 · Introduction to data structures · Medium
A browser must implement a Back operation that returns to the most recently visited page first. Which data structure best matches this requirement?
Not quite. Try another option.
Answer: Stack
A stack follows last-in, first-out order, so the most recently visited page is removed first.
Question 35 of 60 · Types of data structures · Medium
For a sparse graph with vertices and relatively few edges , which representation usually uses space?
Not quite. Try another option.
Answer: Adjacency list
An adjacency list stores each vertex and its incident edges, requiring space rather than .
Question 36 of 60 · Time and space complexity analysis · Medium
What are the time and auxiliary-space complexities of for (int i = 0; i < n; i++) for (int j = 1; j < n; j *= 2) count++;?
Not quite. Try another option.
Answer: time and space
The outer loop runs times, while the inner loop doubles j and runs times. Only constant extra storage is used.
Question 37 of 60 · Time-space trade-off · Medium
A program repeatedly computes an expensive deterministic result for the same inputs. Which change most directly trades additional space for reduced execution time?
Not quite. Try another option.
Answer: Cache previously computed results
Caching stores earlier results in memory so repeated computations can be replaced with faster lookups.
Question 38 of 60 · Big-O notation · Medium
What is the tightest Big-O bound among the choices for ?
Not quite. Try another option.
Answer:
The quadratic term dominates as grows, so the tightest listed asymptotic upper bound is .
Question 39 of 60 · Big-Omega notation · Medium
A loop performs one constant-time operation for every integer from 1 through n. What is its tightest listed asymptotic lower bound?
Not quite. Try another option.
Answer:
The operation is executed exactly times, so the running time grows at least linearly.
Question 40 of 60 · Big-Theta notation · Medium
What is the Big-Theta complexity of for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) count++;?
Not quite. Try another option.
Answer:
The inner loop executes times, which is .
Question 41 of 60 · Introduction to C/C++ · Hard
Assume the following source is compiled once as C and once as C++:
#include <stdlib.h>
int main(void) { int *p = malloc(sizeof *p); free(p); }
Which statement is correct?
Not quite. Try another option.
Answer: It is valid C but ill-formed C++ because C++ does not implicitly convert void * to int *.
C permits implicit conversion from void * to an object-pointer type. C++ requires an explicit conversion. The operand of sizeof is unevaluated, so *p is not actually accessed.
Question 42 of 60 · Data types · Hard
What does the following C++ expression print?
int i = -1; unsigned int u = 1; std::cout << (i < u);
Not quite. Try another option.
Answer: 0, because i is converted to a large unsigned value before comparison.
The usual arithmetic conversions convert i to unsigned int. The converted value is UINT_MAX, which is greater than 1, so the comparison is false.
Question 43 of 60 · Variables · Hard
Given the following C++ program fragment, what is printed?
int x = 1;
void f() { static int x = 2; { int x = 7; ++x; } std::cout << ++x << ' '; }
int main() { f(); f(); std::cout << x; }
Not quite. Try another option.
Answer: 3 4 1
The block variable initialized to 7 is destroyed without affecting the static variable. The static local persists across calls and becomes 3 and then 4; the global x remains 1.
Question 44 of 60 · Constants · Hard
For the declarations below, which statement is well-formed C++?
int a = 1, b = 2; const int *p = &a; int * const q = &a;
Not quite. Try another option.
Answer: p = &b;
p is a modifiable pointer to a constant integer, so it may point elsewhere but cannot modify its target. q cannot be reassigned, and a is not a constant expression.
Question 45 of 60 · Reading and writing data using scanf, printf, cin and cout · Hard
The input consists of 42 followed immediately by a newline. What values result from this C statement?
int n = 0; char c = 'X'; int r = scanf("%d%c", &n, &c);
Not quite. Try another option.
Answer: r is 2, n is 42, and c contains the newline.
%d reads the integer but leaves the following newline unread. %c does not skip whitespace, so it consumes that newline; both conversions succeed.
Question 46 of 60 · Control structures: if-else and loops · Hard
Let , where , and assume arithmetic does not overflow. How many times is work() called?
for (int i = 1; i <= n; i *= 2) for (int j = i; j >= 1; j /= 2) work();
Not quite. Try another option.
Answer:
For outer value , the inner loop executes times. Summing from through gives .
Question 47 of 60 · Functions: declaration and definition · Hard
Which pair of C++ declarations introduces two distinct overloads rather than conflicting declarations or repeated declarations of the same function?
Not quite. Try another option.
Answer: void f(int *); and void f(const int *);
Constness of the pointed-to integer changes the parameter type and permits overloading. Top-level const on value or pointer parameters is ignored in function signatures, while return type alone cannot distinguish overloads.
Question 48 of 60 · Call by value and call by reference · Hard
What is printed by this C++ code?
void transform(int &a, int &b) { a += 2; b *= 3; }
int x = 4; transform(x, x); std::cout << x;
Not quite. Try another option.
Answer: 18
Both references alias the same variable. The first statement changes x from 4 to 6, and the second multiplies that updated value by 3, producing 18.
Question 49 of 60 · Recursion · Hard
For powers of two, what is the time complexity of this function if each addition, comparison, and division costs constant time?
The recurrence is for execution time because adding the numeric value n is still constant-time under the stated model. Thus, the recursion tree has calls, giving time.
Question 50 of 60 · Introduction to object-oriented programming · Hard
What is printed when a Derived object is constructed?
During a base-class constructor, virtual dispatch does not target overrides in a not-yet-constructed derived portion. The call therefore resolves to Base::identify.
Question 51 of 60 · User-defined data types: struct and class · Hard
Consider these C++ definitions:
struct S { int x; }; class C { int x; }; struct D : S {}; class E : S {};
From an unrelated function, for which objects is direct access through object.x permitted?
Not quite. Try another option.
Answer: S and D objects only
struct members and inheritance are public by default, so S::x remains publicly accessible through D. A class has private members and private inheritance by default.
Question 52 of 60 · Classes and objects · Hard
Assuming allocation succeeds, what is printed before scope exit?
class Box { int *p; public: Box(int value) : p(new int(value)) {} void set(int value) { *p = value; } int get() const { return *p; } };
The implicitly generated copy constructor copies the pointer rather than the allocated integer. Both objects therefore refer to the same integer, which b.set(9) modifies.
Question 53 of 60 · Manager functions: constructors and destructors · Hard
Suppose each constructor and destructor records its object's label. For the class below, which sequence is recorded when a D object is created and then destroyed?
struct D : B2, B1 { M m2; M m1; D() : m1("m1"), m2("m2") { record("D"); } ~D() { record("~D"); } };
Not quite. Try another option.
Answer: B2, B1, m2, m1, D, ~D, ~m1, ~m2, ~B1, ~B2
Bases are constructed in base-specifier order, and members in declaration order, regardless of the initializer-list order. Destruction occurs in the exact reverse order after the D destructor body.
Question 54 of 60 · Introduction to data structures · Hard
A collection must support insertion, removal of the most recently inserted value, and retrieval of the current minimum, all in worst-case time. Which design meets every requirement?
Not quite. Try another option.
Answer: A stack whose entries also store the minimum present at insertion time
Each stack entry can store its value and the minimum at or below that entry. Push, pop, and minimum retrieval then require only a constant number of operations.
Question 55 of 60 · Types of data structures · Hard
Which classification correctly describes all four data structures under their conventional implementations?
Not quite. Try another option.
Answer: Array: linear contiguous; linked list: linear noncontiguous; tree: nonlinear hierarchical; graph: nonlinear network
Arrays and linked lists represent linear sequences, though their storage layouts differ. Trees and graphs represent nonlinear relationships, conventionally hierarchical and network-based, respectively.
Question 56 of 60 · Time and space complexity analysis · Hard
Assume work() uses time and space. What are the time and auxiliary-space complexities of this code?
for (int i = 1; i <= n; ++i) for (int j = i; j <= n; j += i) work();
Not quite. Try another option.
Answer: Time and space
For each , the inner loop executes approximately times. The total is , while the loops use only a constant number of variables.
Question 57 of 60 · Time-space trade-off · Hard
A static set of arbitrary keys must answer membership queries. The key universe is too large for direct addressing. Which approach uses extra space and achieves expected total time?
Not quite. Try another option.
Answer: Build a hash table and answer each query with expected constant-time lookup.
A hash table takes expected construction time and expected time per query, using additional storage. The resulting expected total is .
Question 58 of 60 · Big-O notation · Hard
Define
Which asymptotic statement is correct?
Not quite. Try another option.
Answer: but
is an upper bound for every sufficiently large . No positive multiple of is a lower bound on all large odd inputs, where .
Question 59 of 60 · Big-Omega notation · Hard
Let for . Which claim is true?
Not quite. Try another option.
Answer:
The ratio grows without bound, so is an eventual lower bound up to a constant. Dividing by prevents an bound.
Question 60 of 60 · Big-Theta notation · Hard
For any fixed logarithm base greater than , determine the tight asymptotic bound of
Not quite. Try another option.
Answer:
The sum equals . Using gives .
That's unit 1
Answer a few questions and your score shows up here.
Nearly through. Did unit 1 of INT322 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 1 of INT322 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.