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