Worksheetsc++ test
Total questions: 120
Worksheet time: 3600secs
Which characteristic does a good algorithm have?
It must be written in a programming language
It is clear and unambiguous
It must be long and complex
It should have infinite steps
What is the purpose of a flowchart in algorithm design?
To visually represent the steps of an algorithm
To execute an algorithm
To write pseudocode
To debug a program
What is pseudocode?
A low-level machine code
A type of programming language
A high-level representation of an algorithm
A compiled version of an algorithm
What is the main goal of debugging in programming?
To make the code run faster
To compile the program
To add more features to the program
To find and fix errors in a program
What is a computer program?
A hardware component of a computer
A sequence or set of instructions in a programming language for a computer to execute
A physical storage device for programs
A mathematical formula used in software
Why does source code need another program to execute?
Because source code does not contain executable instructions
Because computers can only execute their native machine instructions
Because source code is already in machine language
Because source code cannot be written by humans
What is the purpose of a compiler?
To convert machine instructions back to source code
To store software documentation
To directly execute source code without translation
To translate source code into machine instructions
What was the main reason for the intentionally limited syntax of BASIC (1964)?
To improve execution speed
To make the language easy to learn
To support complex algorithms
To make it compatible with all operating systems
What is a programming language?
A tool used only for web development
A type of hardware that runs computer programs
A collection of pre-written software applications
A set of keywords, symbols, identifiers, and rules for communication with a computer
What is the purpose of syntax in a programming language?
To store large amounts of data
To define rules for structuring code
To improve execution speed
To make programming languages harder to learn
Which of the following is NOT a component of programming language syntax?
Identifiers
Keywords
Symbols
Data compression methods
How are syntax rules typically defined?
Through random trial and error
Using the Backus–Naur form
By copying rules from natural languages
Without any predefined structure
What is an algorithm?
A random set of commands without structure
A method of compressing programming language syntax
A sequence of simple instructions that solve a problem
A type of computer hardware
Which of the following is a correct variable declaration in C++?
variable num;
int num;
declare num;
num int;
Which data type should be used to store a single character in C++?
string
char
character
text
What is the correct way to declare multiple variables of the same type in C++?
variable int a, b, c;
declare int a, b, c;
variable a; b; c;
int a, b, c;
Which operator is used to assign a value to a variable in C++?
=В
==B
:=B
=>B
What is the correct syntax to declare an integer variable in C++?
int=x;
int x;
variable x;
declare inr x;
Which of the following is used to output text in C++?
output()
print()
cout<<
console.log()
What is the correct file extension for a C++ source file?
.cpp
.t
.k++
.kp
How do you correctly start the main function in C++?
main()
int main()
main++
start main()
Which data type is used to store a single character in C++?
string
text
char
character
Which data type is used to store whole numbers without decimals in C++?
string
char
double
int
Which data type is used to store floating-point numbers with decimals in C++?
double
int
char
bool
Which data type is used to store text in C++?
char
string
text
word
Which data type is used to store values with two states (true or false) in C++?
char
string
int
bool
What is the correct syntax for declaring a variable in C++?
type = variableName value;
type variableName = value;
variableName = value;
declare variableName = value;
Which keyword is used to take user input in C++?
input
scanf
cout
cin
Which keyword is used to display output in C++?
cout
cin
display
What is the purpose of the assignment operator (=) in C++?
It multiplies two numbers
It swaps two variables
It assigns a value to a variable
It compares two values
Which of the following is a correct use of the assignment operator?
int x < 10;
10 = x;
int x == 10;
int x = 10;
What will be the value of x after the following statement? int x = 5; x = 10;
10
5
15
Undefined
Which statement about the assignment operator is true?
It checks for equality between two variables
It swaps the values of two variables
It declares a variable without assigning a value
It updates the value stored in a variable
What is the difference between = and == in C++?
= assigns a value, while == compares two values
= compares two values, while == assigns a value
Both perform assignment
Both perform comparison
What is the purpose of comments in C++?
To execute additional lines of code
To speed up program execution
To store variable values
To add explanations or notes in the code that are ignored by the compiler
Which of the following is a single-line comment in C++?
/* This is a comment */
# This is a comment
// This is a comment
-- This is a comment
Which of the following is a multi-line comment in C++?
# This is a comment
// This is a comment
/* This is a comment */
# This is a comment
What happens to comments when a C++ program is compiled?
They slow down the execution of the program
They cause an error if too many are used
They are ignored by the compiler
They are included in the final executable file
What is the correct way to comment out multiple lines in C++?
// This is a multi-line comment //
# This is a multi-line comment #
/* This is a multi-line comment */
-- This is a multi-line comment --
Which of the following statements about comments is true?
Comments are required for the program to run correctly
Comments improve code readability but do not affect execution
Comments execute before the main function
Comments can store variable values
What is the main difference between // and /* ... */ comments?
// is for multi-line comments, while /* ... */ is for single-line comments
// is for single-line comments, while /* ... */ is for multi-line comments
Both can be used interchangeably
Neither is a valid comment syntax in C++
Which of the following is not a valid comment in C++?
// This is a comment
/* This is a comment */
!! This is a comment !!
// Another comment
What is the purpose of an if statement in C++?
To execute a block of code in all cases
To execute a block of code only if a specified condition is true
To declare a variable
To define a function
What will happen if the condition inside an if statement is false?
The compiler will throw an error
The condition will be ignored, and the block will execute
The program will terminate
The block of code inside {} will be skipped
Which of the following is a correct syntax for an if statement in C++?
if x > 5 then { cout << "x is greater than 5"; }
if (x > 5) { cout << "x is greater than 5"; }
if {x > 5} cout << "x is greater than 5";
if x > 5 { cout << "x is greater than 5"; }
What type of value does the condition inside an if statement evaluate to?
Integer only
String
Boolean (true or false)
Character
What is the output of the following code snippet? if (10 < 5) { cout << "x is less than 5"; } cout << "End of program";
End of program
x is less than 5
x is less than 5 End of programВ
Compilation error
Use ________ to specify a block of code to be executed, if a specified condition is true
else
if
switch
loop
Use ________ to specify a block of code to be executed, if the same condition is false
else
switch
continue
break
>Use ________ to specify a new condition to test, if the first condition is false
while
switch
else if
vreak
Use ________ to specify many alternative blocks of code to be executed
return
break
else
switch
Which operator is used to check if one value is less than another in C++?
>=
>
<
<=
Which operator is used to check if one value is less than or equal to another in C++?
<
<=
>=
>
Which operator is used to check if one value is greater than another in C++?
<
>
<=
!=
Which operator is used to check if one value is greater than or equal to another in C++?
<
==
>
>=
Which operator is used to check if two values are equal in C++?
=
==
!=
<=
Which operator is used to check if two values are not equal in C++?
>
==
!=
<
Which operator is used to increase a variable's value by 1 in C++?
+=
++
-=
--
What does the ++ operator do in C++?
Multiplies a variable by 2
Decreases a variable's value by 1
Increases a variable's value by 1
Divides a variable by 2
Which operator is used to decrease a variable's value by 1 in C++?
-=
+=
--
++
What does the -- operator do in C++?
Increases a variable's value by 1
Decreases a variable's value by 1
Multiplies a variable by 2
Divides a variable by 2
What is an array in C++?
A single variable that holds multiple values of different types
A collection of elements of the same type stored in contiguous memory locations
A function that performs calculations
A keyword for declaring variables
How do you declare an integer array of size 5 in C++?
int array[5];
int array[];
array <int> arr(5);
int array = {5};
What is the index of the first element in an array?
1
0
-1
Depends on the array size
What will happen if you access an array index that is out of bounds?
It will return 0 by default
Compiler error
The program will crash
Undefined behavior
Which of the following is NOT a valid array declaration?
float numbers[10];
int arr[5];
double data[] = new double[5];
char str[20];
Which statement correctly initializes an array?
int arr[3]; arr = {1, 2, 3};
int arr = {1, 2, 3};
int arr[3] = {1, 2, 3};
arr[3] = {1, 2, 3};
What will be the output of the following code? int arr[] = {10, 20, 30, 40}; cout << arr[2];
20
30
40
10
How can you find the size of an array in C++?
sizeof(arr) / sizeof(arr[0])
length(arr)
size(arr)
arr.length()
Which loop is commonly used to traverse an array?
if statement
switch statement
do-while loop
for loop
What does the following code output? int arr[3] = {1, 2}; cout << arr[2];
Undefined behavior
0
1
2
How are arrays passed to functions in C++?
They cannot be passed
By reference
By value
As a pointer to the first element
Which function prototype correctly accepts an integer array?
void func(int arr[]);
void func(int* arr);
void func(int arr[10]);
All of the above
What happens when an array is passed to a function?
The entire array is copied
The function receives a pointer to the first element
The function receives only the last element
The function cannot access the array
What is the correct way to modify an array inside a function?
Return the modified array
Pass it by reference using &
Directly modify the passed array
Use a global variable
How do you ensure a function does not modify an array?
Use #define
Copy the array manually
Use static inside the function
Use const before the parameter
Which is the correct declaration of a 2D array?
int matrix[3][3];
int matrix[][];
int matrix{3,3};
matrix arr(3,3);
How do you access the third row, second column of a 2D array?
arr(3,2)
arr[3][2]
arr[2][1]
arr{2,1}
What will be the output of this code? int mat[2][2] = { {1, 2}, {3, 4} }; cout << mat[1][1];
1
2
3
4
Which loop is best suited for iterating over a 2D array?
Single for loop
do-while loop
switch statement
Nested for loop
t does the following code do? int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; cout << arr[0][2];
Outputs 1
Outputs 3
Outputs 4
Outputs 6
Which algorithm is the most efficient for sorting an array?
Bubble Sort
Quick Sort
Selection Sort
Linear Sort
Which sorting algorithm repeatedly swaps adjacent elements?
Merge Sort
Insertion Sort
Radix Sort
Bubble Sort
Which of the following is a disadvantage of linear search?
Works only on sorted arrays
Inefficient for large datasets
Requires recursion
Needs additional memory
Which sorting algorithm divides an array into smaller sub-arrays?
Heap Sort
Selection Sort
Bubble Sort
Merge Sort
What is a loop in C++?
A control structure that repeats a block of code until a condition is met
A variable used for iteration
A function that executes once
A data structure for storing numbers
Which of the following is a type of loop in C++?
Recursion
Conditional loop
While loop
Switch loop
Which loop is best when the number of iterations is known in advance?
While loop
For loop
Do-while loop
If-else statement
What is the correct syntax for a for loop?
for (condition; initialization; update) { // code }
for { initialization; condition; update }
for(initialization; condition; update) { // code }
for condition (initialization; update) { // code }
Which loop always executes at least once?
While loop
Do-while loop
While loop
Nested loop
What is the output of this code? int i = 5; while (i > 0) {cout << i << " ";В В В i--;}
4 3 2 1
5 4 3 2 1
5 4 3 2
Infinite loop
Which of the following is true about while loops?
It executes only if the condition is true
It executes at least once
It is used when the number of iterations is known
It cannot have a break statement
Which loop structure is preferred when a condition must be checked before execution?
Do-while llop
For loop
Recursive function
While loop
What is the correct way to use a break statement in a loop?
loop.break();
if (condition) break;
break = true;
stop loop;
What does the continue statement do in a loop?
Jumps to the end of the program
Executes the loop indefinitely
Stops the loop execution
Skips the current iteration and proceeds to the next one
>How many times will this loop execute? for (int i = 0; i < 5; i++) {cout << i << " ";}
6 times
5 times
4 times
Infinite times
Which keyword is used to exit a loop immediately?
Continue
Return
Break
Exit
Which of the following creates an infinite loop?
do { cout << "Hello"; } while (false);
while (i > 10) { cout << "Hello"; i++; }
for (int i = 0; i < 10; i++) { cout << i; }
while (true) { cout << "Hello"; }
What happens when a loop condition is never false?
The loop executes once
Infinite loop
The program crashes
Compilation error
What is the purpose of nested loops?
To iterate over multi-dimensional data
To execute only one iteration
To stop execution at a certain point
To prevent infinite loops
Which loop is best when the number of iterations is known in advance?
While loop
Do-while llop
Infinite loop
For loop
Which of the following loops executes at least once, even if the condition is false?
While loop
Do-while loop
Nested loop
For loop
What is the syntax of a for loop in C++?
for { initialization; condition; update }
for (condition; initialization; update) { // code }
for(initialization; condition; update) { // code }
for condition (initialization; update) { // code }
What does a while loop check before executing its body?
The condition specified in parentheses
The type of variable being used
The number of iterations
Whether there is a break statement
Which of the following is an infinite loop?
for (int i = 0; i < 5; i++) { cout << i; }
while (i > 10) { cout << "Loop"; i++; }
while (true) { cout << "Looping"; }
do { cout << "Hello"; } while (false);
How many times will the following loop execute? int i = 0; while (i < 3) {cout << i << " ";В В В i++;}
Infinite times
2 times
3 times
4 times
Which statement is used to exit a loop immediately?
Break
Continue
Return
Exit
What does the continue statement do inside a loop?
Exits the program
Stops the loop execution
Restarts the loop from the beginning
Skips the current iteration and moves to the next one
Which loop is best suited for iterating through an array of fixed size?
Recursive function
For loop
While loop
Do-while loop
What will be the output of this for loop? for (int i = 0; i < 4; i++) {cout << i << " ";}
0 1 2 3 4
0 1 2 3
1 2 3 4
4 3 2 1
What is the purpose of nested loops?
To execute only one iteration
To stop execution at a certain point
To iterate over multi-dimensional data
To prevent infinite loops
Which loop is preferred when the number of iterations is unknown?
For loop
While loop
Do-while loop
Switch case
What happens if the condition in a while loop is always true?
The loop executes only once
The loop stops automatically
The program gives a syntax error
The loop runs indefinitely
Which of the following is a valid way to initialize a for loop?
for (int i = 0; i < 10; i++)
for (i = 0; i < 10; i++)
for { int i = 0; i < 10; i++ }
for (int i = 0; i++)
Which of the following correctly represents a do-while loop?
while (condition) { // code }
do { // code } while (condition);
for (initialization; condition; update) { // code }
loop (condition) { // code }
How many times does this loop execute? int i = 10; while (i > 5) {cout << i << " "; i--;}
6 times
4 times
5 times
infinite times
What does the following for loop do? for (int i = 5; i > 0; i--) {cout << i << " ";}
Prints numbers in decreasing order
Prints numbers in increasing order
Prints only 0
Causes an infinite loop
How can you create an infinite for loop?
for (i = 1; i > 0; i++) { cout << "Looping"; }
for (i = 0; i++) { cout << "Looping"; }
for(;;) { cout << "Looping"; }
for (i = 0; i < 100; i--) { cout << "Looping"; }
Which of the following is an example of an off-by-one error in a loop?
Using an incorrect data type
Using i <= size instead of i < size
Using break when not needed
Forgetting to increment the loop variable
What will be the output of this loop? int i = 2; do {cout << i << " "; i++;} while (i < 5);
2 3
2 3 4 5
Infinite loop
2 3 4
