C++ Compiled On Bulu Print
C++ Compiled On Bulu Print
execute. It involves creating algorithms, which are step-by-step sets of instructions that solve a specific
problem or perform a particular task.
Here are some basics of programming to get you started:
Programming Languages: There are numerous programming languages available, each with its own
syntax and purpose. Some popular programming languages include Python, Java, C++, JavaScript, and
Ruby. Each language has its strengths and weaknesses, and the choice of language depends on the task at
hand.
Variables and Data Types:Variables are used to store and manipulate data in a program. Data types
define the kind of data a variable can hold, such as integers, floating-point numbers, strings (text),
Boolean values (true/false), and more. In most programming languages, you need to declare variables
before using them.
Control Structures:Control structures enable you to control the flow of execution in a program.
Common control structures include conditional statements (if-else, switch), loops (for, while), and
branching statements (break, continue).
Functions:Functions are reusable blocks of code that perform specific tasks. They take input parameters
(arguments), perform operations, and optionally return a result. Functions help in modularizing code and
making it more organized and readable.
Data Structures:Data structures are used to organize and manipulate collections of data. Examples of
data structures include arrays (a collection of elements of the same type), linked lists, stacks, queues,
trees, and hash tables. Understanding different data structures is crucial for efficient data manipulation
and algorithm design.
Algorithms: Algorithms are step-by-step procedures or instructions for solving a specific problem. They
can be simple or complex, and their efficiency often determines the performance of a program. Good
algorithm design is essential for writing efficient and scalable programs.
Input and Output:Programs often need to interact with users or read from/write to external sources.
Input refers to receiving data from users or other programs, while output involves displaying or saving
data. Input can come from various sources, such as keyboards, files, or network connections, and output
can be displayed on the screen, saved to files, or sent over networks.
Debugging: Debugging is the process of identifying and fixing errors or bugs in a program. It involves
techniques such as reading error messages, using debugging tools, and applying systematic approaches to
locate and resolve issues.
pg. 1
Object-Oriented Programming (OOP):OOP is a programming paradigm that focuses on organizing
code around objects that encapsulate data and behavior. It emphasizes concepts like classes, objects,
inheritance, polymorphism, and encapsulation. Languages like Java, C++, and Python support OOP.
Databases:Databases are used to store, manage, and retrieve data efficiently. Understanding database
concepts, relational databases (e.g., SQL), and NoSQL databases (e.g., MongoDB) is important for
building data-driven applications.
Web Development:Web development involves building websites and web applications. It encompasses
front-end development (HTML, CSS, JavaScript) for creating user interfaces, and back-end development
for server-side logic and data processing. Frameworks like React, Angular, Vue.js, and Node.js are
commonly used in web development.
Mobile App Development:Mobile app development focuses on creating applications for mobile
devices such as smartphones and tablets. Platforms like iOS (using Swift or Objective-C) and Android
(using Java or Kotlin) have their specific development environments and frameworks.
Algorithms and Data Structures: As mentioned earlier, understanding algorithms and data structures
is crucial for efficient programming. Dive deeper into topics like sorting algorithms, searching algorithms,
graph algorithms, dynamic programming, and more.
Version Control:Version control systems like Git enable you to manage and track changes to your
codebase. Learning how to use version control is essential for collaboration, code management, and
tracking project history.
Testing and Debugging Techniques:In addition to debugging, understanding testing techniques like
unit testing, integration testing, and test-driven development (TDD) helps ensure the reliability and
correctness of your code.
Security:As you develop software, it's important to be aware of common security vulnerabilities and
best practices for securing your applications, such as input validation, secure authentication, and
protection against common attacks like cross-site scripting (XSS) and SQL injection.
pg. 2
a) Python
b) CSS
c) Java
d) C++
Answer: b) CSS
What is a variable in programming?
a) A reserved word in a programming language
b) A device used for storing data
c) A named container for storing data
d) A special function in a program
Answer: c) A named container for storing data
Which programming construct is used to repeat a set of instructions multiple times?
a) Function
b) Array
c) Class
d) Loop
Answer: d) Loop
What is the purpose of a conditional statement in programming?
a) To declare variables
b) To store data in memory
c) To perform mathematical calculations
d) To make decisions based on a condition
Answer: d) To make decisions based on a condition
Which data type is used to represent true or false values in programming?
a) Integer
b) String
c) Boolean
d) Float
Answer: c) Boolean
What is the process of finding and fixing errors in a program called?
pg. 3
a) Debugging
b) Compiling
c) Executing
d) Testing
Answer: a) Debugging
Which programming paradigm focuses on organizing code around objects?
a) Functional programming
b) Procedural programming
c) Object-oriented programming
d) Imperative programming
Answer: c) Object-oriented programming
Which of the following is a popular web development framework for JavaScript?
a) Django
b) Ruby on Rails
c) Express.js
d) Flask
Answer: c) Express.js
What is the purpose of a function in programming?
a) To store data
b) To define a class
c) To perform a specific task
d) To create an object
Answer: c) To perform a specific task
Which of the following is NOT a fundamental data structure?
a) Stack
b) Queue
c) Tree
d) Loop
Answer: d) Loop
Which programming language is often used for scientific computing and data analysis?
pg. 4
a) Python
b) C#
c) Ruby
d) PHP
Answer: a) Python
What does HTML stand for?
a) Hypertext Markup Language
b) Hyper Tool Markup Language
c) High-level Markup Language
d) Home Markup Language
Answer: a) Hypertext Markup Language
Which of the following is NOT a type of loop?
a) for loop
b) if loop
c) while loop
d) do-while loop
Answer: b) if loop
Which keyword is used to declare a variable in most programming languages?
a) var
b) const
c) let
d) int
Answer: a) var
What is the output of the following code snippet: print(2 + 3 * 4)?
a) 20
b) 14
c) 15
d) 9
Answer: c) 15
Which of the following is NOT a step in the software development process?
pg. 5
a) Requirement analysis
b) Design
c) Debugging
d) Maintenance
Answer: c) Debugging
Which programming language is often used for building Android apps?
a) Swift
b) Java
c) C#
d) Kotlin
Answer: d) Kotlin
Which of the following is NOT a relational database management system?
a) MySQL
b) MongoDB
c) Oracle
d) PostgreSQL
Answer: b) MongoDB
Which symbol is used for single-line comments in many programming languages?
a) //
b) --
c) /*
d) #
Answer: a) //
What is the purpose of version control systems like Git?
a) To organize code into classes and objects
b) To store and manage database records
c) To track changes to code and collaborate with others
d) To execute and run code
Answer: c) To track changes to code and collaborate with others
pg. 6
Keywords:Keywords are reserved words in a programming language that have predefined meanings and
cannot be used as variable names or identifiers. Examples of keywords in languages like Python include
"if," "for," "while," "class," and "return."
Identifiers:Identifiers are names given to variables, functions, classes, or other program elements. They
are used to refer to these elements within the program. In most programming languages, identifiers must
follow certain rules, such as starting with a letter or underscore, and can consist of letters, digits, and
underscores. For example, "myVariable," "calculateSum," and "Person" are identifiers.
Inputs:Inputs are data or values provided to a program during its execution. They can be provided by the
user, read from files, retrieved from a database, or obtained from other sources. Inputs can be used as
variables or parameters within the program to perform calculations, make decisions, or produce outputs.
Outputs:Outputs are the results or data produced by a program after processing the inputs. They can be
displayed to the user, stored in files, sent to other programs, or used further within the program. Outputs
can be in the form of text, numbers, images, or any other data type relevant to the program's purpose.
Comments:Comments are lines of text within a program that are not executed as part of the code. They
are used to provide explanations, document code, and make it more readable for other programmers or for
future reference. Comments help in understanding the purpose of code snippets, logic, or any important
details. In many programming languages, comments start with a specific symbol or keyword, such as "#"
in Python for single-line comments or "/* ... */" in C/C++ for multi-line comments.
Parts of a Program:A program typically consists of various parts that work together to perform a
specific task. These parts may include:
pg. 7
What are keywords in a programming language?
a) Special words with predefined meanings
b) Names given to variables and functions
c) Text used to explain code logic
d) Values provided to a program during execution
Answer: a
Which of the following is a valid identifier?
a) 123identifier
b) _identifier
c) Ident@fier
d) identifier#123
Answer: b
Inputs in a program are:
a) Reserved words in a programming language
b) Data or values provided during program execution
c) Comments used to explain code logic
d) Results produced by a program
Answer: b
What are outputs in a program?
a) Special words with predefined meanings
b) Data or results produced by a program
c) Names given to variables and functions
d) Text used to explain code logic
Answer: b
What is the purpose of comments in a program?
a) To provide a name for a program element
b) To store data or values during program execution
c) To explain code logic and improve readability
pg. 8
Which part of a program is responsible for declarations?
a) Control structures
b) Input handling
c) Declarations
d) Output handling
Answer: c
Functions or methods in a program are:
a) Reserved words in a programming language
b) Blocks of code that perform specific tasks
c) Results produced by a program
d) Data or values provided during program execution
Answer: b
Which part of a program is responsible for processing logic?
a) Declarations
b) Input handling
c) Processing logic
d) Output handling
Answer: c
Which part of a program handles unexpected errors or exceptions?
a) Control structures
b) Input handling
c) Error handling
d) Output handling
Answer: c
Which operator is used for assigning values to variables?
a) ==
b) >=
c) =
d) !=
Answer: c
pg. 9
Which operator is used for equality comparison?
a) >=
b) =
c) ==
d) !=
Answer: c
Which operator is used for logical AND?
a) ||
b) &&
c) !
d) ^
Answer: b
Which operator is used for addition?
a) +
b) *
c) /
d) %
Answer: a
Which operator is used for division?
a) %
b) +
c) /
d) *
Answer: c
Which operator is used for finding the remainder?
a) %
b) /
c) +
d) *
Answer: a
pg. 10
Which operator is used for concatenating strings?
a) +
b) -
c) *
d) /
Answer: a
Which operator is used for exponentiation?
a) +
b) *
c) /
d) **
Answer: d
Which operator is used for greater than or equal to comparison?
a) =
b) ==
c) >=
d) <=
Answer: c
Which operator is used for logical OR?
a) &&
b) ||
c) !
d) ^
Answer: b
Which operator is used for bitwise AND?
a) &
b) |
c) ^
d) <<
Answer: a
pg. 11
Which operator is used for bitwise OR?
a) &
b) |
c) ^
d) <<
Answer: b
Which operator is used for bitwise XOR?
a) &
b) |
c) ^
d) <<
Answer: c
Which operator is used for left shift?
a) &
b) |
c) ^
d) <<
Answer: d
Which operator is used for right shift?
a) &
b) |
c) ^
d) >>
Answer: d
Which operator is used for greater than comparison?
a) >
b) <
c) >=
d) <=
Answer: a
pg. 12
Which operator is used for less than comparison?
a) >
b) <
c) >=
d) <=
Answer: b
Which keyword is used to define a function in Python?
a) function
b) def
c) define
d) func
Answer: b
Which of the following is a valid identifier in most programming languages?
a) 123identifier
b) _identifier
c) ident@fier
d) identifier#123 Answer: b
What is the purpose of inputs in a program?
a) To print output to the console
b) To store data during program execution
c) To retrieve data from external sources
d) To define the structure of a program Answer: c
Outputs in a program are used for:
a) Error handling
b) Storing intermediate results
c) Displaying results to the user
d) Declaring program variables
Answer: c
pg. 13
What is the primary use of comments in a program?
a) To execute specific code blocks
b) To improve the performance of a program
c) To provide explanations and documentation
d) To define the flow of program execution
Answer: c
Which part of a program is responsible for controlling the flow of execution?
a) Declarations
b) Control structures
c) Processing logic
d) Input handling
Answer: b
Which part of a program is responsible for handling errors or exceptions?
a) Control structures
b) Input handling
c) Error handling
d) Output handling
Answer: c
Which operator is used for greater than comparison?
a) >
b) <
c) >=
d) <= Answer: a
Which operator is used for less than or equal to comparison?
a) >
b) <
c) >=
d) <=Answer: d
pg. 14
Which operator is used for logical NOT?
a) &&
b) ||
c) !
d) ^
Answer: c
Which operator is used for multiplication?
a) +
b) *
c) /
d) %
Answer: b
Which operator is used for division?
a) %
b) +
c) /
d) *
Answer: c
Which operator is used for finding the remainder?
a) %
b) /
c) +
d) * Answer: a
Which operator is used for string concatenation in Python?
a) +
b) -
c) *
d) / Answer: a
pg. 15
Which operator is used for exponentiation in Python?
a) +
b) *
c) /
d) ** Answer: d
Which operator is used for greater than or equal to comparison?
a) =
b) ==
c) >=
d) <= Answer: c
Which operator is used for logical OR?
a) &&
b) ||
c) !
d) ^ Answer: b
Which operator is used for bitwise AND?
a) &
b) |
c) ^
d) <<Answer: a
Which operator is used for bitwise OR?
a) &
b) |
c) ^
d) <<
Answer: b
pg. 16
Which operator is used for bitwise XOR?
a) &
b) |
c) ^
d) <<Answer: c
Which operator is used for left shift?
a) &
b) |
c) ^
d) <<
Answer: d
Which operator is used for right shift?
a) &
b) |
c) ^
d) >>
Answer: d
Which operator is used for not equal to comparison?
a) ==
b) >=
c) =
d) !=
Answer: d
Which of the following is a valid data type in most programming languages?
a) Numeric
b) Text
c) Boolean
d) All of the above
Answer: d
pg. 17
A variable is a named storage location that can hold a value, and its value can:
a) Change during program execution
b) Never change once assigned
c) Be assigned only once
d) None of the above
Answer: a
Which symbol is commonly used to assign a value to a variable?
a) =
b) ==
c) +
d) *
Answer: a
What is the purpose of declaring variables in a program?
a) To assign a value to the variable
b) To reserve memory for the variable
c) To define the data type of the variable
d) All of the above
Answer: d
Which of the following is a valid variable name?
a) 123variable
b) variable_123
c) var@iable
d) variable#123
Answer: b
Constants are used to store values that:
a) Can be changed during program execution
b) Remain the same throughout the program
c) Are assigned to variables
d) Are used for control structures
Answer: b
pg. 18
Which of the following is a valid constant?
a) PI
b) 3.14
c) "Hello, World!"
d) All of the above
Answer: d
What is the purpose of data types in programming?
a) To determine the size of a variable
b) To define the operations that can be performed on a variable
c) To allocate memory for a variable
d) All of the above
Answer: d
Which data type is used to store whole numbers in most programming languages?
a) Integer
b) Float
c) String
d) Boolean
Answer: a
Which data type is used to store decimal numbers in most programming languages?
a) Integer
b) Float
c) String
d) Boolean
Answer: b
Which data type is used to store characters in most programming languages?
a) Integer
b) Float
c) String
d) Boolean
Answer: c
pg. 19
Which data type is used to store true/false values in most programming languages?
a) Integer
b) Float
c) String
d) Boolean
Answer: d
Which arithmetic operator is used for addition?
a) +
b) -
c) *
d) /
Answer: a
Which arithmetic operator is used for subtraction?
a) +
b) -
c) *
d) /
Answer: b
Which arithmetic operator is used for multiplication?
a) +
b) -
c) *
d) /
Answer: c
Which arithmetic operator is used for division?
a) +
b) -
c) *
d) /
Answer: d
pg. 20
Which operator is used for equality comparison?
a) >
b) <
c) ==
d) !=
Answer: c
Which operator is used for greater than comparison?
a) >
b) <
c) ==
d) !=
Answer: a
pg. 21
d) ^
Answer: b
Which operator is used for logical NOT?
a) &&
b) ||
c) !
d) ^
Answer: c
Which operator is used for exponentiation?
a) +
b) *
c) /
d) **
Answer: d
Which operator is used for modulus (remainder) calculation?
a) %
b) /
c) *
d) **
Answer: a
Which operator is used for string concatenation?
a) + b) - c) * d) /
Answer: a
Which operator is used for assignment?
a) == b) = c) :=d) +=Answer: b
What is the data type of the value "Hello, World!"?
a) Integer b) Float c) String d) Boolean
Answer: c
Which operator is used for greater than or equal to comparison?
a) = b) == c) >= d) <=Answer: c
pg. 22
Which operator is used for not equal to comparison?
a) = b) == c) >= d) !=
Answer: d
Which operator is used for incrementing a variable by 1?
a) ++ b) += c) = d) *
Answer: a
Which operator is used for decrementing a variable by 1?
a) ++ b) += c) = d) -
Answer: d
What is the result of the expression 10 / 3 in most programming languages?
a) 3 b) 3.33 c) 3.0 d) 4
Answer: b
What is the result of the expression 10 // 3 in most programming languages?
a) 3 b) 3.33 c) 3.0 d) 4
Answer: a
What is the result of the expression "Hello" + "World!"?
a) HelloWorld! b) Hello World! c) Hello+World! d) Error
Answer: b
Which operator is used for exponentiation in most programming languages?
a) + b) * c) / d) **
Answer: d
Which operator is used for bitwise AND?
a) & b) | c) ^ d) <<
Answer: a
Which operator is used for bitwise OR?
a) & b) | c) ^ d) <<
Answer: b
Which operator is used for bitwise XOR?
a) & b) | c) ^ d) <<
Answer: c
pg. 23
Which operator is used for left shift in most programming languages?
a) & b) | c) ^ d) <<
Answer: d
Which operator is used for right shift in most programming languages?
a) & b) | c) ^ d) >>
Answer: d
Which operator is used for compound assignment (adding and assigning)?
a) +=
b) -=
c) /=
d) *=
Answer: a
Which operator is used for compound assignment (subtracting and assigning)?
a) +=
b) -=
c) /=
d) *=
Answer: b
Which operator is used for compound assignment (dividing and assigning)?
a) += b) -= c) /= d) *=
Answer: c
Which operator is used for compound assignment (multiplying and assigning)?
a) += b) -= c) /= d) *=
Answer: d
Which operator is used for finding the remainder of a division in most programming languages?
a) % b) / c) * d) **
Answer: a
Which operator is used for comparing two values and returning true if they are equal?
a) > b) < c) == d) !=
Answer: c
pg. 24
Which operator is used for comparing two values and returning true if they are not equal?
a) > b) < c) == d) !=
Answer: d
What is the value of the expression 5 < 3 in most programming languages?
a) true b) false c) 5 d) 3
Answer: b
What is the value of the expression 5 > 3 && 2 < 4?
a) true
b) false
c) 5
d) 3
Answer: a
What is the value of the expression !(5 > 3)?
a) true
b) false
c) 5
d) 3
Answer: b
pg. 25
2.8.5. Increment and Decrement Operators:
Increment and decrement operators are used to increase or decrease the value of a variable by one.
The increment operator is represented by "++" and the decrement operator by "--". They can be
used both as prefix operators (++x) and postfix operators (x++). The difference between prefix and
postfix operators is the order in which the increment or decrement operation is performed.
2.8.6. Infix and Postfix Types:
Infix and postfix are not types of operators, but rather different ways of representing expressions.
Infix notation is the conventional mathematical notation where operators are placed between
operands, such as "2 + 3". Postfix notation, also known as Reverse Polish Notation (RPN), is a
notation where operators are placed after their operands, such as "2 3 +". Postfix notation is
commonly used in stack-based calculators and some programming languages.
2.8.7. Precedence of Operators:
Operators have different levels of precedence, which determines the order in which operations are
performed in an expression. For example, multiplication and division have a higher precedence
than addition and subtraction. If operators with the same precedence appear in an expression, their
associativity determines the order of evaluation. Associativity can be left-to-right or right-to-left.
It's important to consult the specific documentation or language specification of the programming
language you are using, as the details and behavior of these operators may vary.
Additional note
2.8.2. Compound Assignment Operators:
Compound assignment operators provide a shorthand way to combine an arithmetic or bitwise
operation with assignment. Instead of writing separate statements for performing an operation and
assigning the result, you can use compound assignment operators to perform the operation and
assignment in a single step. For example, instead of writing "x = x + 5", you can use the compound
assignment operator "+=" and write "x += 5".
Compound assignment operators work by taking the value of the variable on the left-hand side,
performing the specified operation with the value on the right-hand side, and then assigning the
result back to the variable on the left-hand side. For example, the compound assignment operator
"+=" adds the value on the right-hand side to the value of the variable on the left-hand side and
assigns the result back to the variable.
2.8.3. Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations on numerical operands.
Common arithmetic operators include:
pg. 26
Addition (+): Adds two operands together.
Subtraction (-): Subtracts the right operand from the left operand.
Multiplication (*): Multiplies two operands together.
Division (/): Divides the left operand by the right operand.
Modulo (%): Returns the remainder of the division operation.
These operators can be used with different numerical data types, such as integers and floating-point
numbers, depending on the programming language.
2.8.4. Relational Operators:
Relational operators are used to compare the relationship between two values and return a Boolean
result (true or false). Common relational operators include:
Greater than (>): Checks if the left operand is greater than the right operand.
Less than (<): Checks if the left operand is less than the right operand.
Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right
operand.
Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Equal to (==): Checks if the left operand is equal to the right operand.
Not equal to (!=): Checks if the left operand is not equal to the right operand.
Relational operators are often used in conditional statements (such as if statements) or loops to
make decisions based on the comparison results.
pg. 27
Infix notation: In infix notation, operators are placed between the operands. For example, "2 + 3"
is an infix expression where the operator "+" is placed between the operands 2 and 3. Infix notation
is the most common way of writing mathematical expressions.
Postfix notation: In postfix notation, also known as Reverse Polish Notation (RPN), operators are
placed after their operands. For example, "2 3 +" is a postfix expression where the operator "+" is
placed after the operands 2 and 3. Postfix notation eliminates the need for parentheses to indicate
the order of operations. It is often used in stack-based calculators and some programming
languages.
2.8.7. Precedence of Operators:
Operators have different levels of precedence, which determines the order in which operations are
performed in an expression. When an expression contains multiple operators, the one with higher
precedence is evaluated first. If operators have the same precedence, their associativity, which can
be left-to-right or right-to-left, determines the order of evaluation.
For example, in the expression "2 + 3 * 4", the multiplication operator (*) has a higher precedence
than the addition operator (+), so the multiplication is performed first. The result is then added to
2.
It's important to understand the precedence and associativity rules of operators in the
programming language you are using to ensure that expressions are evaluated correctly.
pg. 28
c. /
d. %
Answer: b
Which relational operator checks if the left operand is less than the right operand?
a. <
b. >
c. <=
d. >=
Answer: a
What does the increment operator (++x) do in C++?
a. Adds 1 to the value of x
b. Subtracts 1 from the value of x
c. Multiplies the value of x by 2
d. Divides the value of x by 2
Answer: a
Which postfix operator decrements the value of a variable by one?
a. --
b. ++
c. +=
d. -=
Answer: a
In postfix notation, where are operators placed in relation to their operands?
a. Before the operands
b. After the operands
c. Between the operands
d. Both before and after the operands
Answer: b
Which operator has the highest precedence in C++?
a. +
b. *
pg. 29
c. /
d. %
Answer: b
What is the associativity of the assignment operator (=) in C++?
a. Left-to-right
b. Right-to-left
c. No associativity
d. Depends on the context
Answer: a
Which operator is used to perform integer division and round down to the nearest integer?
a. /
b. %
c. //
d. ^
Answer: c
What is the purpose of the compound assignment operator "&=" in C++?
a. Performs bitwise AND and assignment
b. Performs logical AND and assignment
c. Performs bitwise OR and assignment
d. Performs logical OR and assignment
Answer: a
Which arithmetic operator raises a number to a power in C++?
a. ^
b. **
c. //
d. *
Answer: a
Which relational operator is used to check if two values are equal in C++?
a. == b. = c. != d. =
Answer: a
pg. 30
What does the expression "x += 5" do in C++?
a. Adds 5 to the value of x and assigns the result back to x
b. Subtracts 5 from the value of x and assigns the result back to x
c. Multiplies the value of x by 5 and assigns the result back to x
d. Divides the value of x by 5 and assigns the result back to x
Answe: a
Which operator has the lowest precedence in C++?
a. +
b. -
c. *
d. /
Answer: b
What is the result of the expression "10 % 3" in C++?
a. 1
b. 2
c. 3
d. 0
Answer: b
What is the purpose of the relational operators in C++?
a. To perform arithmetic operations with assignment
b. To compare two values and return a Boolean result
c. To increase or decrease the value of a variable by one
d. To represent expressions with operators placed after their operands
Answer: b
Which operator is used for string concatenation in some programming languages?
a. +
b. *
c. /
d. -
Answer: a
pg. 31
What does the decrement operator (x--) do in C++?
a. Adds 1 to the value of x
b. Subtracts 1 from the value of x
c. Multiplies the value of x by 2
d. Divides the value of x by 2
Answer: b
In infix notation, where are operators placed in21. What is the purpose of compound assignment
operators in C++?
a. To perform arithmetic operations with assignment in a single step
b. To compare two values and return a Boolean result
c. To increase or decrease the value of a variable by one
d. To represent expressions with operators placed after their operands
Answer: a
Which compound assignment operator is used to subtract a value from a variable and assign the
result back to the variable?
a. -=
b. +=
c. *=
d. /=
Answer: a
Which arithmetic operator is used to multiply two operands together?
a. +
b. *
c. /
d. %
Answer: b
Which relational operator checks if the left operand is less than the right operand?
a. <
b. >
c. <=
d. >=
pg. 32
Answer: a
What does the increment operator (++x) do in C++?
a. Adds 1 to the value of x
b. Subtracts 1 from the value of x
c. Multiplies the value of x by 2
d. Divides the value of x by 2
Answer: a
Which postfix operator decrements the value of a variable by one?
a. --
b. ++
c. +=
d. -=
Answer: a
In postfix notation, where are operators placed in relation to their operands?
a. Before the operands
b. After the operands
c. Between the operands
d. Both before and after the operands
Answer: b
Which operator has the highest precedence in C++?
a. +
b. *
c. /
d. %
Answer: b
What is the associativity of the assignment operator (=) in C++?
a. Left-to-right
b. Right-to-left
c. No associativity
d. Depends on the context
pg. 33
Answer: a
Which operator is used to perform integer division and round down to the nearest integer?
a. /
b. %
c. //
d. ^
Answer: c
What is the purpose of the compound assignment operator "&=" in C++?
a. Performs bitwise AND and assignment
b. Performs logical AND and assignment
c. Performs bitwise OR and assignment
d. Performs logical OR and assignment
Answer: a
Which arithmetic operator raises a number to a power in C++?
a. ^
b. **
c. //
d. *
Answer: a
Which relational operator is used to check if two values are equal in C++?
a. ==
b. =
c. !=
d. =
Answer: a
What does the expression "x += 5" do in C++?
a. Adds 5 to the value of x and assigns the result back to x
b. Subtracts 5 from the value of x and assigns the result back to x
c. Multiplies the value of x by 5 and assigns the result back to x
d. Divides the value of x by 5 and assigns the result back to x
pg. 34
Answer: a
Which operator has the lowest precedence in C++?
a. +
b. -
c. *
d. /
Answer: b
What is the result of the expression "10 % 3" in C++?
a. 1
b. 2
c. 3
d. 0
Answer: b
What is the purpose of the relational operators in C++?
a. To perform arithmetic operations with assignment
b. To compare two values and return a Boolean result
c. To increase or decrease the value of a variable by one
d. To represent expressions with operators placed after their operands
Answer: b
Which operator is used for string concatenation in some programming languages?
a. +
b. *
c. /
d. - Answer: a
What does the decrement operator (x--) do in C++?
a. Adds 1 to the value of x
b. Subtracts 1 from the value of x
c. Multiplies the value of x by 2
d. Divides the value of x by 2
Answer: b
pg. 35
In infix notation, where are operators placed in relation to their operands?
a. Before the operands
b. After the operands
c. Between the operands
d. Both before and after the operands
Answer: c
python
Copy
if condition1:
# code to be executed if condition1 is True
elif condition2:
# code to be executed if condition2 is True
else:
# code to be executed if all conditions are False
pg. 36
The conditions are evaluated in order. If condition1 is True, the code inside its block is executed. If
condition1 is False and condition2 is True, the code inside the corresponding block is executed. If
all conditions are False, the code inside the else block is executed.
python
Copy
for item in sequence:
# code to be executed for each item
The loop variable "item" takes each value from the sequence, one at a time, and the code inside the
loop is executed with that value.
python
Copy
while condition:
# code to be executed while the condition is True
The code inside the loop is executed repeatedly as long as the condition evaluates to True. If the
condition becomes False, the program exits the loop and continues with the next statement after the
loop.
pg. 37
Python does not have a built-in do...while statement like some other programming languages.
However, you can achieve similar functionality using a while loop with a condition that is checked
at the end of the loop. This ensures that the code inside the loop is executed at least once, regardless
of the condition.
Here's an example:
python
Copy
while True:
# code to be executed
if condition:
break
The code inside the loop will be executed at least once, and then the condition is checked at the end.
If the condition is met, the loop is exited using the break statement.
pg. 38
Answer: c
In the if...else statement, which block of code is executed if the condition is false?
a) if block b) else block
c) elseif block d) None of the above
Answer: b
What is the syntax for the if...else statement?
a) if (condition) { // code } b) if condition: # code
c) if (condition) then // code else // code d) if condition: // code else: // code
Answer: b
Which control statement is used to repeatedly execute a block of code until a condition is true?
a) if...else statement b) for loop statement
c) switch statement d) while loop statement
Answer: d
Which control statement is used to execute a block of code at least once?
a) if...else statement b) for loop statement
c) do...while statement d) while loop statement
Answer: c
Which control statement is used to select one of many code blocks to be executed?
a) if...else statement b) for loop statement
c) switch statement d) while loop statement
Answer: a
What is the basic syntax for the for loop statement in Python?
a) for item in sequence: // code b) for (item in sequence) { // code }
c) for item = 0 to length(sequence) { // code } d) for (item = 0; item < length(sequence); item++) { //
code }
Answer: a
Which control statement is used to exit a loop prematurely?
a) break statement b) continue statement
c) return statement d) exit statement
Answer: a
pg. 39
In the if...elseif...else statement, which block of code is executed if all conditions are false?
a) if block b) else block
c) elseif block d) None of the above
Answer: b
What is the purpose of the switch statement?
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To check multiple conditions d) To iterate over a sequence
Answer: b
Which control statement is used to execute a block of code repeatedly with a post-execution
condition check?
a) if...else statement b) for loop statement
c) while loop statement d) switch statement
Answer: c
In the do...while statement, when is the condition checked?
a) Before executing the code block b) After executing the code block
c) Both before and after executing the code block d) None of the above
Answer: b
What is the syntax for the switch statement in Python?
a) switch (expression) { case value: // code } b) switch expression: // code
c)switch { case value: // code } d) Python does not have a built-in switch statement
Answer: d
Which control statement is used to execute a block of code repeatedly for a specified number of
times?
a) if...else statement b) for loop statement
c) while loop statement d) switch statement
Answer: b
What is the syntax for the while loop statement in Python?
a) while condition: // code b) while (condition) { // code }
c) while { condition: // code } d) while (condition) then // code else // code
Answer: a
19Here are the remaining questions:
pg. 40
Which control statement is used to skip the current iteration of a loop and move to the next
iteration?
a) break statement b) return statement
c) continue statement d) exit statement
Answer: c
Which control statement is used to exit the current loop entirely?
a) break statement b) return statement
c) continue statement d) exit statement
Answer: a
What happens if there is no matching case in a switch statement?
a) An error is thrown b) The program continues to the next case
c) The default case is executed d) The program terminates
Answer: c
How many times does a do...while loop execute its code block?
a) At most once b) At least once
c) It depends on the condition d) It executes indefinitely
Answer: b
Which control statement is used to return a value from a function?
a) break statement b) return statement
c) continue statement d) exit statement
Answer: b
What is the purpose of the else statement in the if...else statement?
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To check multiple conditions d) To execute code when the if condition is false
Answer: d
Which control statement is used to exit the entire program?
a) breakstatement b) return statement
c) continue statement d) exit statement
Answer: d
What is the purpose of the continue statement?
pg. 41
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To skip the current iteration of a loop d) To exit the entire program
Answer: c
How does the for loop statement know when to stop iterating?
a) It stops when the condition is false b) It stops when the break statement is encountered
c) It stops when the specified number of iterations is reached d) It stops when the continue
statement is encountered
Answer: c
Which control statement is used to execute a block of code based on a range of values?
a) if...else statement b) for loop statement
c) switch statement d) while loop statement
Answer: b
In the do...while statement, when is the condition checked?
a) Before executing the code block b) After executing the code block
c) Both before and after executing the code block d) None of the above
Answer: b
How many conditions can be checked in an if...elseif...else statement?
a) Only one b) Multiple
c) It depends on the programming language d) None of the above
Answer: b
What happens if no condition in the if...elseif...else statement is true?
a) An error is thrown b) The program continues to the next condition
c) The else block is executed d) The program terminates
Answer: c
What is the purpose of the while loop statement?
a) To execute a block of code repeatedlyb) To select one of many code blocks to be executed
c) To check multiple conditions d) To iterate over a sequence
Answer: a
Which control statement is used to execute a block of code repeatedly for an unknown number of
times?
a) if...else statement b) for loop statement
pg. 42
c) while loop statement d) switch statement
Answer: c
How is the do...while statement different from the while loop statement?
a) The do...while statement always executes at least once b) The while loop statement always
executes at least once
c) The do...while statement does not support conditions d) The while loop statement does not
support conditions
Answer: a
Which control statement is used to check multiple conditions in a concise way?
a) if...else statement b) for loop statement
c) switch statement d) while loop statement
Answer: c
What is the purpose of the break statement?
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To exit the current loop d) To exit the entire program
Answer: c
What is the purpose of the continue statement?
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To skip the current iteration of a loop d) To exit the entire program
Answer: c
How does the for loop statement know when to stop iterating?
a) It stops when the condition is false
b) It stops when the break statement is encountered
c) It stops when the specified number of iterations is reached
d) It stops when the continue statement is encountered
Answer: c
Which control statement is used to execute a block of code based on a range of values?
a) if...else statement b) for loop statement
c) switch statement d) while loop statement
Answer: b
pg. 43
In the do...while statement, when is the condition checked?
a) Before executing the code block b) After executing the code block
c) Both before and after executing the code block d) None of the above
Answer: b
How many conditions can be checked in an if...elseif...else statement?
a) Only one b) Multiple
c) It depends on the programming language d) None of the above
Answer: b
What happens if no condition in the if...elseif...else statement is true?
a) An error is thrown b) The program continues to the next condition
c) The else block is executed d) The program terminates
Answer: c
What is the purpose of the while loop statement?
a) To execute a block of code repeatedly
b) To select one of many code blocks to be executed
c) To check multiple conditions
d) To iterate over a sequence
Answer: a
Which control statement is used to execute a block of code repeatedly for an unknown number of
times?
a) if...else statement b) for loop statement
c) while loop statement d) switch statement
Answer: c
How is the do...while statement different from the while loop statement?
a) The do...while statement always executes at least once
b) The while loop statement always executes at least once
c) The do...while statement does not support conditions
d) The while loop statement does not support conditions
pg. 44
Answer: a
Which control statement is used to check multiple conditions in a concise way?
a) if...else statement b) for loop statement c) switch statement d) while loop statement
Answer: c
What is the purpose of the break statement?
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To exit the current loop d) To exit the entire program
Answer: c
What is the purpose of the continue statement?
a) To execute a block of code repeatedly b) To select one of many code blocks to be executed
c) To skip the current iteration of a loop d) To exit the entire program
Answer: c
What is the difference between the while loop and the do...while loop?
a) The while loop executes the code block at least once, while the do...while loop may not execute it
at all.
b) The while loop checks the condition before executing the code block, while the do...while loop
checks the condition after executing the code block.
c) The while loop is used for iterating over a sequence, while the do...while loop is used for
executing a block of code repeatedly.
d) There is no difference; they are two different names for the same loop structure.
Answer: b
Scope of Variables:
Control statements can also affect the scope of variables. In many programming languages,
variables declared within a control statement block are only accessible within that block. For
example, in the following code snippet:
pg. 45
python
Copy
if condition:
x=5
print(x)
print(x) # Error: x is not defined
```
The variable `x` is only accessible within the `if` block, and trying to access it outside of that block
will result in an error.
python
Copy
if condition1:
# Code block for condition1
elif condition2:
# Code block for condition2
else:
# Code block for when neither condition1 nor condition2 is true
```
In this example, the `if` statement is nested within an `else` statement, providing multiple levels of
decision-making.
pg. 46
javascript
Copy
switch (value) {
case 1:
// Code block for value 1
break;
case 2:
// Code block for value 2
break;
default:
// Code block for all other values
break;
}
```
The switch statement evaluates the value and executes the code block corresponding to the
matching case. If none of the cases match, the default code block is executed.
python
Copy
try:
# Code that may raise an exception
exceptExceptionType:
# Code to handle the exception
else:
pg. 47
# Code to execute if no exception occurred
finally:
# Code that always executes, regardless of exceptions
```
The `try` block contains the code that might raise an exception. If an exception occurs, the `catch`
block handles it. If no exception occurs, the `else` block is executed. The `finally` block is optional
and always executes, regardless of whether an exception occurred or not.
pg. 48
````cpp
void increment(intnum) {
num += 1;
}
int x = 5;
increment(x);
cout<< x <<endl; // Output: 5
```
In this example, the value of `x` is passed to the `increment` function. Inside the function, a copy of
the value is stored in the `num` parameter. When `num` is incremented, it does not affect the
original value of `x`.
4.4 Passing Value to a Function by Reference:
In C++, you can pass a value to a function by reference using references or pointers. When a value
is passed by reference, any modifications made to the parameter inside the function will affect the
original value. Here's an example:
````cpp
void increment(int&num) {
num += 1;
}
int x = 5;
increment(x);
cout<< x <<endl; // Output: 6
```
In this example, the `increment` function takes an integer reference `num`. Changes made to `num`
directly affect the original value of `x` because it is passed by reference.
pg. 49
````cpp
void increment(int* num) {
(*num) += 1;
}
int x = 5;
increment(&x);
cout<< x <<endl; // Output: 6
```
In this example, the `increment` function takes an integer pointer `num`, and the value of `x` is
passed by taking its address using the `&` operator.
Remember that passing by reference can be more efficient when dealing with large objects, as it
avoids making unnecessary copies.
I hope this clarifies passing arguments to functions in C++. If you have any further questions, feel
free to ask!
What is a function?
a) A variable
b) A loop
c) A named block of code that performs a specific task
d) An operator
Answer: c
What is the purpose of a function declaration?
a) To define a function
b) To call a function
c) To declare the existence of a function
d) To return a value
Answer: c
pg. 50
Which part of a function declaration specifies the function name?
a) Return type
b) Parameters
c) Function body
d) None of the above
Answer: d
The return type of a function determines:
a) The number of parameters the function can accept
b) The value the function returns
c) The visibility of the function
d) None of the above
Answer: b
How do you define a function in C++?
a) By using the "define" keyword
b) By using the "function" keyword
c) By using the "def" keyword
d) By providing the function name, return type, parameters, and function body
Answer: d
What keyword is used to declare a function in C++?
a) func
b) def
c) void
d) None of the above
Answer: d
How do you pass a value to a function by value in C++?
a) By using a pointer
b) By using a reference
c) By using the "const" keyword
d) By directly passing the value
Answer: d
pg. 51
When a value is passed to a function by value, any modifications made to the parameter inside the
function:
a) Affect the original value
b) Create a new variable
c) Do not affect the original value
d) Generate a compilation error
Answer: c
How do you pass a value to a function by reference in C++?
a) By using the "ref" keyword
b) By using a pointer
c) By using a reference
d) By directly passing the value
Answer: c
When a value is passed to a function by reference, any modifications made to the parameter inside
the function:
a) Affect the original value
b) Create a new variable
c) Do not affect the original value
d) Generate a compilation error
Answer: a
What is the advantage of passing by reference over passing by value?
a) It allows for more efficient memory usage
b) It provides stronger type checking
c) It improves code readability
d) There is no advantage; they are equivalent
Answer: a
Which operator is used to access the address of a variable in C++?
a) *
b) &
c) $
d) %
pg. 52
Answer: b
What is the purpose of a function prototype?
a) To define a function
b) To call a function
c) To declare the existence of a function
d) To return a value
Answer: c
In C++, can a function be called before it is declared?
a) Yes
b) No
Answer: b
What is the scope of a function parameter in C++?
a) Local to the function
b) Global to the program
c) Local to the block where the parameter is defined
d) Local to the class
Answer: a
Can a function have multiple return statements in C++?
a) Yes
b) No
Answer: a
Which of the following is not a valid return type for a function in C++?
a) int
b) void
c) float[]
d) char*
Answer: c
What is the purpose of the "void" keyword in a function declaration?
a) It indicates that the function does not return a value
b) It indicates that the function accepts no parameters
pg. 53
c) It indicates that the function has no body
d) It indicates that the function is a constructor
Answer: a
Can a function have no return type in C++?
a) Yes
b) No
Answer: b
What happens if a function is declared with a return type but does not return a value?
a) The program crashes
b) The compiler generates a warning
c) The compiler generates an error
d) The function returns a defaultvalue
Anwer: c
How do you pass an array to a function in C++?
a) By using a pointer
b) By using a reference
c) By using the "const" keyword
d) By directly passing the array
Answer: a
What is function overloading in C++?
a) Defining multiple functions with the same name but different parameters
b) Defining multiple functions with the same name and the same parameters
c) Defining a function inside another function
d) Defining a function that calls itself
Answer: a
Can two functions have the same name and the same parameters in C++?
a) Yes
b) No
Answer: b
What is a default argument in C++?
pg. 54
a) An argument that is automatically passed to a function
b) An argument with a default value that can be omitted when calling a function
c) An argument that is required for the function to execute correctly
d) An argument that is passed by reference
Answer: b
How do you specify a default argument in a function declaration?
a) By using the "default" keyword
b) By using the "optional" keyword
c) By assigning a value to the parameter in the declaration
d) By using the "default" modifier
Answer: c
What is function recursion in C++?
a) Calling a function multiple times from within itself
b) Calling a function from another function
c) Calling a function with a different set of arguments
d) Calling a function with a different return type
Answer: a
Which of the following is true about recursion?
a) Recursion is always more efficient than iteration
b) Recursion can lead to infinite loops if not implemented correctly
c) Recursion can only be used with void functions
d) Recursion can only be used with functions that have a return type
Answer: b
What is the maximum depth of recursion in C++?
a) It depends on the size of the stack
b) It is fixed at 100 levels
c) It is fixed at 1000 levels
d) There is no maximum depth
Answer: a
What is a function template in C++?
pg. 55
a) A function that accepts a variable number of arguments
b) A function that can be used with different data types
c) A function that has a return type of "template"
d) A function that is defined within a class template
Answer: b
How do you define a function template in C++?
a) By using the "template" keyword followed by the template parameter list
b) By using the "function" keyword followed by the template parameter list
c) By using the "template" keyword followed by the function name
d) By using the "template" keyword followed by the return type
Answer: a
What is a function pointer in C++?
a) A pointer that stores the memory address of a function
b) A pointer that can only be used in function definitions
c) A pointer that stores the return value of a function
d) A pointer that can only be used with predefined functions
Answer: a
How do you declare a function pointer in C++?
a) By using the "pointer" keyword b) By using the "&" operator
c) By using the "*" operator d) By using the "function" keyword
Answer: c
What is the purpose of the "const" keyword in a function declaration?
a) It indicates that the function cannot be called
b) It indicates that the function does not return a value
c) It indicates that the function cannot modify its parameters
d) It indicates that the function has no body
Answer: c
Can a function have multiple parameters with the same name in C++?
a) Yes
b) No
pg. 56
Answer: b
What is function hiding in C++?
a) When a derived class function hides the base class function
b) When a function is defined inside another function
c) When a function is declared but not defined d) When a function is declared but never called
Answer: a
How do you call a function recursively in C++?
a) By using a loop b) By using the "recursive" keyword
c) By calling the function from within itself d) By using a function pointer
Answer: c
Can a function call itself directly in C++?
a) Yes
b) No
Answer: a
What is the process of passing arguments to a function called?
a) Argument declaration b) Argument passing
c) Argument initialization d) Argument assignment
Answer: b
Which type of argument passing involves creating a copy of the argument to be passed?
a) Pass by value b) Pass by reference
c) Pass by pointer d) Pass by const
Answer: a
Which type of argument passing allows modification of the original argument within the function?
a) Pass by value b) Pass by reference
c) Pass by pointer d) Pass by const
Answer: b
Which type of argument passing is more efficient when working with large data structures?
a) Pass by value b) Pass by reference
c) Pass by pointer d) Pass by const
pg. 57
Answer: b
Which type of argument passing allows the function to access and modify the original value via its
memory address?
a) Pass by value b) Pass by reference
c) Pass by pointer d) Pass by const
Answer: c
In C++, how do you pass arguments by reference?
a) Precede the parameter name with an ampersand (&) in the function declaration
b) Precede the argument with an ampersand (&) when calling the function
c) Use the "ref" keyword before the parameter type in the function declaration
d) Use the "ref" keyword before the argument type when calling the function
Answer: a
In C++, how do you pass arguments by pointer?
a) Precede the parameter name with an asterisk () in the function declaration
b) Precede the argument with an asterisk () when calling the function
c) Use the "ptr" keyword before the parameter type in the function declaration
d) Use the "ptr" keyword before the argument type when calling the function
Answer: b
In C++, how do you pass arguments by const reference?
a) Precede the parameter name with the "const" keyword in the function declaration
b) Precede the argument with the "const" keyword when calling the function
c) Use the "constref" keyword before the parameter type in the function declaration
d) Use the "constref" keyword before the argument type when calling the function
Answer: a
Which of the following is true about passing arguments by value?
a) The original argument cannot be modified within the function
b) The function receives a copy of the argument
c) The function can modify the original argument directly
d) The function can modify the original argument through its memory address
Answer: b
pg. 58
Which of the following is true about passing arguments by reference?
a) The original argument cannot be modified within the function
b) The function receives a copy of the argument
c) The function can modify the original argument directly
d) The function can modify the original argument through its memory address
Answer: c
Which of the following is true about passing arguments by pointer?
a) The original argument cannot be modified within the function
b) The function receives a copy of the argument
c) The function can modify the original argument directly
d) The function can modify the original argument through its memory address
Answer: d
Which of the following is true about passing arguments by const reference?
a) The original argument cannot be modified within the function
b) The function receives a copy of the argument
c) The function can modify the original argument directly
d) The function can modify the original argument through its memory address
Answer: a
What is the purpose of passing arguments by const reference?
a) To prevent the function from modifying the original argument
b) To allow the function to modify the original argument efficiently
c) To improve code readability
d) To reduce the memory usage of the function
Answer: a
Which type of argument passing is commonly used for passing large data structures to functions?
a) Pass by value b) Pass by reference
c) Pass by pointer d) Pass by const
Answer: b
Which type of argument passing is commonly used for passing arrays to functions?
a) Pass by value b) Pass by reference
pg. 59
c) Pass by pointer d) Pass by const
Answer: c
In C++, a structure is a user-defined data type that allows you to combine different variables of different
types into a single unit. It is similar to a class, but with default public access to its members. Structures
are used to represent a collection of related data items.
Here's an example of declaring and using a structure in C++:
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
float height;
};
int main() {
// Declare a structure variable
Person person1;
pg. 60
In the above example, the Person structure is defined with three members: name (a string), age (an
integer), and height (a float). In the main function, a variable person1 of type Person is declared and
initialized with values. The structure members are accessed using the dot (.) operator.
Structures can also have member functions, just like classes in C++. However, by default, the members
of a structure are public, whereas in a class, they are private by default.
Note that in modern C++, the use of structures is often replaced by classes, as classes provide additional
features such as encapsulation and inheritance.
Defining a Structure: You can define a structure using the struct keyword, followed by the structure
name and a block of member declarations enclosed in curly braces. For example:
struct Point {
int x;
int y;
};
```
Initializing a Structure: You can initialize a structure variable at the time of declaration using braces {}
or using the assignment operator (=). For example:
Point p1 = {10, 20}; // Initializing with braces
Point p2;
p2 = {30, 40}; // Initializing with assignment operator
```
Accessing Structure Members: You can access the members of a structure using the dot (.) operator. For
example:
p1.x = 50;
cout << "x-coordinate: " << p1.x << endl;
```
Passing Structures to Functions: You can pass structure variables to functions by value or by reference.
When passed by value, a copy of the structure is made, while passing by reference allows you to modify
the original structure. For example:
void displayPoint(Point p) {
cout << "x-coordinate: " << p.x << endl;
cout << "y-coordinate: " << p.y << endl;
}
void modifyPoint(Point& p) {
pg. 61
p.x = 100;
p.y = 200;
}
int main() {
Point p = {50, 60};
displayPoint(p);
modifyPoint(p);
displayPoint(p);
return 0;
}
```
Nested Structures: Structures can be nested inside other structures. This allows you to create more
complex data structures. For example:
struct Date {
int day;
int month;
int year;
};
struct Person {
string name;
Date dob;
};
Person person;
person.name = "John Doe";
person.dob.day = 10;
person.dob.month = 5;
person.dob.year = 1990;
```
Typedef for Structures: You can use the typedef keyword to create an alias for a structure type. This
makes it easier to declare variables of that structure type. For example:
pg. 62
typedef struct {
string make;
string model;
int year;
} Car;
Car myCar;
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2022;
```
These are some of the key aspects of structures in C++. Structures provide a way to organize related data
into a single unit and can be useful in various programming scenarios.
Structure Size: The size of a structure in memory is determined by the sizes of its individual members.
The compiler may add padding bytes between members for alignment purposes. The sizeof operator can
be used to determine the size of a structure in bytes. For example:
struct Point {
int x;
int y;
};
cout << "Size of Point structure: " << sizeof(Point) << " bytes" << endl;
```
Arrays of Structures: You can create arrays of structures in C++. This allows you to store multiple
instances of a structure in a contiguous block of memory. For example:
struct Student {
string name;
int age;
};
Student students[5];
students[0].name = "John";
students[0].age = 20;
```
pg. 63
Pointers to Structures: Pointers can be used to store the memory address of a structure. You can then
access the structure members using the arrow (->) operator. For example:
struct Rectangle {
int length;
int width;
};
Rectangle* rectPtr; // Pointer to Rectangle structure
rectPtr = new Rectangle; // Dynamically allocate memory for the structure
rectPtr->length = 10;
rectPtr->width = 5;
delete rectPtr; // Deallocate memory
```
Structure Initialization with Constructors: C++11 introduced the ability to initialize a structure using
constructors. Constructors are special member functions that are called when an object is created. Here's
an example:
struct Point {
int x;
int y;
// Constructor
Point(int a, int b) : x(a), y(b) {}
};
Point p(10, 20); // Initialize Point using constructor
```
Structure Inheritance: C++ supports structure inheritance, where you can derive a new structure from
an existing one. The derived structure inherits all the members of the base structure and can add
additional members. Inheritance is achieved using the colon (:) notation. For example:
struct Shape {
int x;
int y;
};
struct Rectangle : Shape {
int width;
pg. 64
int height;
};
Rectangle rect;
rect.x = 5;
rect.y = 10;
rect.width = 20;
rect.height = 30;
```
Structure as Function Return Type: You can return a structure from a function in C++. This can be
useful when you want to return multiple values from a function. For example:
struct Point {
int x;
int y;
};
Point getPoint() {
Point p;
p.x = 10;
p.y = 20;
return p;
}
Point result = getPoint();
```
These are some additional aspects of structures in C++. Structures provide flexibility in organizing and
manipulating related data, and they can be used in various programming scenarios to improve code
organization and readability.
pg. 65
Which keyword is used to define a structure in C++?
a. class b. struct
c. typedef d. data
Answer: b. struct
How do you access a member variable age of a structure Person?
a. Person.age b. age.Person
c. Person::age d. person.age
Answer: d. person.age
What is the size of an empty structure in C++?
a. 0 bytes b. 1 byte
c. 4 bytes d. It depends on the compiler.
Answer: b. 1 byte
Which operator is used to access structure members through a pointer?
a. -> b. *
c. . d. ::
Answer: a. ->
What is the purpose of padding in a structure?
a. To increase the size of the structure b. To align structure members in memory
c. To improve structure performance d. Padding is not used in structures.
Answer: b. To align structure members in memory
How can you initialize a structure at the time of declaration?
a. Using the initialize keyword b. Using the init function
c. Using the assignment operator (=) d. Structures cannot be initialized at declaration.
Answer: c. Using the assignment operator (=)
Which of the following is true about structures in C++?
a. Structures cannot have member functions. b. Structures are always passed by reference.
c. Structures cannot contain other structures. d. Structures have default public access.
Answer: d. Structures have default public access.
Which keyword is used to create an alias for a structure type?
a. alias b. typedef
pg. 66
c. structdef d. typealias
Answer: b. typedef
Which operator is used to access structure members in a nested structure?
a. b. ->
c. :: d. :
Answer: a. .
What happens if you pass a structure to a function by value?
a. The original structure remains unchanged. b. The function receives a copy of the structure.
c. The function receives a pointer to the structure. d. The program throws a compilation error.
Answer: b. The function receives a copy of the structure.
How can you define a structure with a constructor in C++?
a. By using the new keyword b. By using the construct keyword
c. By defining a member function with the structure name d. Structures cannot have constructors.
Answer: c. By defining a member function with the structure name
What is the size of the following structure: struct Example { int a; char b; double c; };?
a. 7 bytes b. 8 bytes
c. 12 bytes d. It depends on the compiler.
Answer: c. 12 bytes
Which of the following statements about arrays of structures is correct?
a. All structures in an array must have the same size. b. Arrays of structures cannot be initialized.
c. Structures cannot be elements of an array. d. Array elements can be accessed using the dot operator.
Answer: d. Array elements can be accessed using the dot operator.
How can you dynamically allocate memory for a structure in C++?
a. Using the malloc function b. Using the new keyword
c. Structures cannot be dynamically allocated. d. By declaring the structure as a pointer.
Answer: b. Using the new keyword
Which of the following statements about structure inheritance is true?
a. A structure can inherit from multiple base structures. b. Inheritance is not supported in structures.
c. Base structures inherit members from derived structures. d. Derived structures can add new members
to the base structure.
pg. 67
Answer: b. Inheritance is not supported in structures.
How can you return a structure from a function in C++?
a. By using the return statement b. By passing the structure as a parameter
c. Structures cannot be returned from functions. d. By returning a pointer to the structure
Answer: a. By using the return statement
Which of the following statements about nested structures is true?
a. Nested structures cannot have member functions.
b. Nested structures have access to private members of the outer structure.
c. Nested structures cannot be accessed outside of the outer structure.
d. Nested structures cannot contain pointers as members.
Answer: b. Nested structures have access to private members of the outer structure.
What is the purpose of the sizeof operator in C++?
a. It returns the size of a structure in bytes.
b. It returns the number of elements in an array.
c. It calculates the sum of all elements in a structure.
d. It is used to compare the sizes of two structures.
Answer: a. It returns the size of a structure in bytes.
Which of the following statements about anonymous structures is true?
a. Anonymous structures cannot have member variables.
b. Anonymous structures cannot be used as function parameters.
c. Anonymous structures do not have a name.
d. Anonymous structures cannot be initialized.
Answer: c. Anonymous structures do not have a name.
How do you access a member variable salary of a structure Employee through a pointer empPtr?
a. empPtr.salary
b. (*empPtr).salary
c. empPtr->salary
d. empPtr::salary
Answer: c. empPtr->salary
pg. 68
Which of the following is true about structure variables in C++?
a. Structure variables are always initialized with default values.
b. Structure variables cannot be assigned values after declaration.
c. Structure variables can be declared without specifying the structure name.
d. Structure variables can only store integer values.
Answer: c. Structure variables can be declared without specifying the structure name.
What is the output of the following code?
struct Point {
int x;
int y;
};
pg. 69
Which of the following statements about structure initialization is true?
a. Structures cannot be initialized at all.
b. Structures can only be initialized using constructor functions.
c. Structures can be initialized using an initializer list.
d. Structures can only be initialized using the assignment operator (=).
Answer: c. Structures can be initialized using an initializer list.
What is the purpose of the offsetof macro in C++?
a. It returns the offset of a member within a structure.
b. It returns the number of bytes allocated for a structure.
c. It determines the size of a structure.
d. It is used to define a nested structure.
Answer: a. It returns the offset of a member within a structure.
How can you compare two structures for equality in C++?
a. Using the == operator b. Using the is_equal() function
c. Structures cannot be compared for equality. d. By comparing each member individually.
Answer: c. Structures cannot be compared for equality.
What is the purpose of the union keyword in C++?
a. It defines a structure with multiple members. b. It defines a structure with default values.
c. It defines a structure that can store different types of data. d. It defines a structure with private access
modifiers.
Answer: c. It defines a structure that can store different types of data.
Which of the following statements about bit-fields in structures is true?
a. Bit-fields are always aligned on byte boundaries. b. Bit-fields have a minimum size of one byte.
c. Bit-fields can only store integer values. d. Bit-fields cannot be used in structures.
Answer: b. Bit-fields have a minimum size of one byte.
How can you define a constant structure member in C++?
a. By using the const keyword before the member name. b. By using the static keyword before the
member name.
c. Constants cannot be used as structure members. d. By declaring the member as const in the structure
definition.
Answer: a. By using the const keyword before the member name.
pg. 70
Which of the following statements about structure alignment is true?
a. Structure alignment ensures that structures always occupy the same amount of memory.
b. Structure alignment is the process of arranging structure members in ascending order.
c. Structure alignment can be controlled using the __attribute__((aligned)) directive.
d. Structure alignment has no effect on the memory layout of structures.
Answer: c. Structure alignment can be controlled using the __attribute__((aligned)) directive.
What is the output of the following code?
struct Point {
int x;
int y;
};
Point p1 = {10, 20};
Point p2 = p1;
p2.x = 30;
cout << p1.x << endl;
a. 10 b. 20
c. 30 d. Error: Invalid syntax
Answer: a. 10
Which of the following is a valid way to access a structure member using a reference variable?
a. refVar.member b. refVar->member
c. (*refVar).member d. refVar::member
Answer: a. refVar.member
What is the purpose of the static keyword in a structure declaration?
a. It makes the structure members accessible from outside the structure. b. It makes the structure a global
variable.
c. It makes the structure members shared among all instances of the structure. d. The static keyword
cannot be used in a structure declaration.
Answer: c. It makes the structure members shared among all instances of the structure.
pg. 71
Which of the following statements about nested unions is true?
a. Nested unions cannot have member functions.
b. Nested unions can only be accessed through the outer union.
c. Nested unions cannot contain other nested unions.
d. Nested unions have access to private members of the outer union.
Answer: b. Nested unions can only be accessed through the outer union.
What is the purpose of the typedef keyword in C++?
a. It is used to define a new type alias for an existing data type.
b. It is used to create a new structure.
c. It is used to declare a member variable within a structure.
d. It is used to access the members of a structure.
Answer: a. It is used to define a new type alias for an existing data type.
Which of the following statements about arrays of structures is true?
a. Arrays of structures cannot be passed to functions.
b. Arrays of structures cannot be initialized at the time of declaration.
c. Arrays of structures cannot have variable size.
d. Arrays of structures can be accessed using the dot operator (.) only.
Answer: b. Arrays of structures cannot be initialized at the time of declaration.
What is the purpose of the this pointer in C++?
a. It is used to access the address of the current structure object.
b. It is used to access the members of a nested structure.
c. It is used to refer to the base class object within a derived class.
d. It is used to create a self-referential structure.
Answer: a. It is used to access the address of the current structure object.
Which of the following statements about structure inheritance is true?
a. Structures cannot inherit from other structures.
b. Structure inheritance is only possible between structures of the same type.
c. Structure inheritance allows a structure to inherit the members of another structure.
d. Structure inheritance is a concept specific to object-oriented programming.
pg. 72
Answer: c. Structure inheritance allows a structure to inherit the members of another structure.
What is the output of the following code?
struct Point {
int x;
int y;
};
Point arr[3] = {{1, 2}, {3, 4}, {5, 6}};
cout << arr[1].y << endl;
a. 1 b. 2
c. 3 d. 4
Answer: d. 4
Which of the following statements about structure pointers is true?
a. Structure pointers cannot be used to access structure members.
b. Structure pointers cannot be compared for equality.
c. Structure pointers cannot be dereferenced.
d. Structure pointers can be used to dynamically allocate memory for structures.
Answer: d. Structure pointers can be used to dynamically allocate memory for structures.
What is the purpose of the sizeof operator when used with a structure?
a. It returns the size of the structure in bytes.
b. It returns the number of members in the structure.
c. It calculates the sum of all member sizes in the structure.
d. It is used to compare the sizes of two structures.
Answer: a. It returns the size of the structure in bytes.
What is the output of the following code?
struct Point {
int x;
int y;
};
Point p = {10, 20};
Point* ptr = &p;
pg. 73
cout << (*ptr).y << endl;
a. 10 b. 20
c. Error: Cannot access members through a pointer d. Garbage value
Answer: b. 20
Which of the following is a valid way to pass a structure to a function by value?
a. void func(struct Point p) b. void func(struct Point* p)
c. void func(struct& Point p) d. void func(struct& Point* p)
Answer: a. void func(struct Point p)
What is the purpose of the offsetof macro in C++?
a. It returns the offset of a member within a structure. b. It returns the number of bytes allocated for a
structure.
c. It determines the size of a structure. d. It is used to define a nested structure.
Answer: a. It returns the offset of a member within a structure.
Which of the following statements about structure packing is true?
a. Structure packing ensures that structures always occupy the same amount of memory.
b. Structure packing is the process of arranging structure members in ascending order.
c. Structure packing can be controlled using the __attribute__((packed)) directive.
d. Structure packing has no effect on the memory layout of structures.
Answer: c. Structure packing can be controlled using the __attribute__((packed)) directive.
What is the output of the following code?
struct Person {
char name[10];
int age;
};
Person p;
cout << sizeof(p) << endl;
a. 10 b. 14
c. 16 d. Error: Invalid syntax
Answer: c. 16
pg. 74
How can you pass a structure to a function by reference in C++?
a. By using the & operator in the function argument.
b. By using the * operator in the function argument.
c. Structures cannot be passed by reference in C++.
d. By using the sizeof operator in the function argument.
Answer: a. By using the & operator in the function argument.
What is the purpose of the typedef keyword in C++?
a. It is used to define a new type alias for an existing data type.
b. It is used to create a new structure.
c. It is used to declare a member variable within a structure.
d. It is used to access the members of a structure.
Answer: a. It is used to define a new type alias for an existing data type.
What is the output of the following code?
struct Point {
int x;
int y;
};
Point p = {10, 20};
Point* ptr = &p;
cout << ptr->x << endl;
a. 10 b. 20
c. Error: Cannot access members through a pointer d. Garbage value
Answer: a. 10
Which of the following statements about structure initialization is true?
a. Structures cannot be initialized at all.
b. Structures can only be initialized using constructor functions.
c. Structures can be initialized using an initializer list.
d. Structures can only be initialized using the assignment operator (=).
Answer: c. Structures can be initialized using an initializer list.
pg. 75
What is the purpose of the this pointer in C++?
a. It is used to access the address of the current structure object.
b. It is used to access the members of a nested structure.
c. It is used to refer to the base class object within a derived class.
d. It is used to create a self-referential structure.
Answer: a. It is used to access the address of the current structure object.
What is the output of the following code?
struct Rectangle {
int length;
int width;
};
Rectangle r = {5, 6};
Rectangle* ptr = new Rectangle;
ptr->length = 10;
ptr->width = 8;
cout << r.length << " " << r.width << endl;
cout << ptr->length << " " << ptr->width << endl;
delete ptr;
a. 10 8 5 6 b. 5 6 10 8 c. 5 6 Garbage value Garbage value
d. Error: Invalid syntax
Answer: b. 5 6 10 8
Which of the following statements about nested structures is true?
a. Nested structures cannot have member functions.
b. Nested structures can only be accessed through the outer structure.
c. Nested structures cannot contain other nested structures.
d. Nested structures have access to private members of the outer structure.
Answer: d. Nested structures have access to private members of the outer structure.
pg. 76
What is the purpose of the union keyword in C++?
a. It is used to define a new structure. b. It is used to create a new data type that can hold different types
of data.
c. It is used to declare a member variable within a structure. d. It is used to access the members of a
structure.
Answer: b. It is used to create a new data type that can hold different types of data.
Which of the following statements about structure and class in C++ is true?
a. Structures and classes are exactly the same in C++.
b. Structures can have member functions and constructors, just like classes.
c. Classes can have public and private members, while structures can only have public members.
d. Structures and classes have different default access specifiers (public vs. private).
Answer: d. Structures and classes have different default access specifiers (public vs. private).
What is the output of the following code?
struct Person {
int age;
char name[20];
};
Person p1 = {25, "John"};
Person p2 = p1;
p2.age = 30;
cout << p1.age << " " << p2.age << endl;
a. 25 25 b. 30 30
c. 25 30 d. 30 25
Answer: c. 25 30
Which of the following statements about constant structures in C++ is true?
a. Constant structures cannot have any member variables.
b. Constant structures can only have const member variables.
c. Constant structures cannot be modified after declaration.
d. Constant structures can only be accessed using the arrow operator (->).
Answer: c. Constant structures cannot be modified after declaration.
pg. 77
What is the output of the following code?
struct Point {
int x;
int y;
};
Point arr[3] = {1, 2, 3, 4, 5, 6};
cout << arr[1].y << endl;
a. 1 b. 2
c. 3 d. 4
Answer: d. 4
Which of the following statements about structure pointers in C++ is true?
a. Structure pointers cannot be used to access structure members.
b. Structure pointers cannot be compared for equality.
c. Structure pointers cannot be dereferenced.
d. Structure pointers can be used to dynamically allocate memory for structures.
Answer: d. Structure pointers can be used to dynamically allocate memory for structures.
What is the output of the following code?
struct Rectangle {
int length;
int width;
};
Rectangle r = {5, 6};
Rectangle* ptr = &r;
cout << (*ptr).length << endl;
a. 5 b. 6
c. Error: Cannot access members through a pointer d. Garbage value
Answer: a. 5
Which of the following is a valid way to pass a structure to a function by reference in C++?
a. void func(struct Point p) b. void func(struct Point* p)
c. void func(struct& Point p) d. void func(struct& Point* p)
pg. 78
Answer: b. void func(struct Point* p)
What is the purpose of the sizeof operator when used with a structure?
a. It returns the size of the structure in bytes. b. It returns the number of members in the structure.
c. It calculates the sum of all member sizes in the structure. d. It is used to compare the sizes of two
structures.
Answer: a. It returns the size of the structure in bytes.
What is the output of the following code?
struct Point {
int x;
int y;
};
Point p = {10, 20};
Point* ptr = &p;
cout << ptr->y << endl;
a. 10 b. 20
c. Error: Cannot access members through a pointer d. Garbage value
Answer: b. 20
What is the purpose of the const keyword in C++?
a. It specifies that a variable cannot be modified. b. It specifies that a function cannot be called.
c. It specifies that a class cannot be inherited. d. It specifies that a structure cannot have member
functions.
Answer: a. It specifies that a variable cannot be modified.
What is the output of the following code?
int x = 5;
int* ptr = &x;
*ptr = 10;
cout << x << endl;
a. 5 b. 10
c. Error: Invalid syntax d. Garbage value
Answer: b. 10
pg. 79
Which of the following statements about references in C++ is true?
a. References must be initialized when declared and cannot be changed to refer to a different object.
b. References can only refer to global variables.
c. References can be null.
d. References are the same as pointers in terms of usage and behavior.
Answer: a. References must be initialized when declared and cannot be changed to refer to a different
object.
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr + 2;
cout << *ptr << endl;
a. 1 b. 2
c. 3 d. 4
Answer: c. 3
What is the purpose of the virtual keyword in C++?
a. It is used to define pure virtual functions.
b. It is used to indicate that a function can be overridden in a derived class.
c. It is used to declare a base class.
d. It is used to restrict access to a class member.
Answer: b. It is used to indicate that a function can be overridden in a derived class.
What is the output of the following code?
int x = 5;
int& ref = x;
ref = 10;
cout << x << endl;
a. 5 b. 10
c. Error: Invalid syntax d. Garbage value
Answer: b. 10
Which of the following statements about inheritance in C++ is true?
a. Multiple inheritance allows a class to inherit from multiple base classes.
pg. 80
b. Private inheritance allows access to the public and protected members of the base class.
c. Inheritance is not supported in C++.
d. Derived classes cannot override the member functions of the base class.
Answer: a. Multiple inheritance allows a class to inherit from multiple base classes.
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *(ptr + 3) << endl;
a. 1 b. 2
c. 3 d. 4
Answer: d. 4
Which of the following statements about function overloading in C++ is true?
a. Function overloading allows multiple functions with the same name but different return types.
b. Function overloading is not supported in C++.
c. Function overloading allows multiple functions with the same name but different parameter lists.
d. Function overloading requires defining a different name for each overloaded function.
Answer: c. Function overloading allows multiple functions with the same name but different parameter
lists.
What is the output of the following code?
int x = 5;
int* ptr = &x;
int** ptr2 = &ptr;
cout << **ptr2 << endl;
a. 5 b. 10
c. Error: Invalid syntax d. Garbage value
Answer: a. 5
What is the purpose of the static keyword in C++?
a. It is used to declare a variable that can only be accessed within a specific function.
b. It is used to indicate that a class cannot be inherited.
c. It is used to define a function that belongs to a class rather than an instance of the class.
pg. 81
d. It is used to declare a variable that retains its value between function calls.
Answer: d. It is used to declare a variable that retains its value between function calls.
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << ptr[2] << endl;
a. 1 b. 2
c. 3 d. 4
Answer: c. 3
Which of the following statements about the new operator in C++ is true?
a. The new operator is used to allocate memory for an array.
b. The new operator is used to deallocate memory.
c. The new operator is used to call a constructor.
d. The new operator is used to initialize a variable.
Answer: a. The new operator is used to allocate memory for an array.
What is the output of the following code?
int x = 5;
int& ref = x;
int* ptr = &ref;
cout << *ptr << endl;
a. 5 b. 10
c. Error: Invalid syntax d. Garbage value
Answer: a. 5
Which of the following statements about polymorphism in C++ is true?
a. Polymorphism allows objects of a derived class to be treated as objects of its base class.
b. Polymorphism is not supported in C++.
c. Polymorphism allows a class to inherit from multiple base classes.
d. Polymorphism requires the use of virtual functions.
Answer: a. Polymorphism allows objects of a derived class to be treated as objects of its base class.
pg. 82
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr + 3;
cout << *ptr << endl;
a. 1 b. 2
c. 3 d. 4
Answer: d. 4
Which of the following statements about the delete operator in C++ is true?
a. The delete operator is used to allocate memory.
b. The delete operator is used to deallocate memory.
c. The delete operator is used to call a destructor.
d. The delete operator is used to initialize a variable.
Answer: b. The delete operator is used to deallocate memory.
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
int* ptr2 = ptr + 2;
cout << *ptr2 << endl;
a. 1
b. 2
c. 3
d. 4
Answer: c. 3
Which of the following statements about the const keyword in C++ is true?
a. The const keyword can only be applied to primitive data types.
b. The const keyword is used to declare a variable that can be modified.
c. The const keyword is used to indicate that a function cannot be called.
d. The const keyword is used to declare a variable that cannot be modified.
Answer: d. The const keyword is used to declare a variable that cannot be modified.
pg. 83
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = &arr[3];
cout << *(ptr - 2) << endl;
a. 1 b. 2
c. 3 d. 4
Answer: b. 2
What is the purpose of the try-catch block in exception handling?
a. It ensures that the program terminates if an exception occurs.
b. It handles and recovers from exceptions that occur during program execution.
c. It prevents exceptions from occurring in the program.
d. It is used to declare and define custom exception classes.
Answer: b. It handles and recovers from exceptions that occur during program execution.
What is the output of the following code?
int x = 5;
int y = 0;
int result = 0;
try {
result = x / y;
} catch (const std::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
std::cout << result << std::endl;
a. 0 b. 1
c. Error: Division by zero d. Garbage value
Answer: c. Error: Division by zero
Which of the following statements about object-oriented programming (OOP) is true?
a. OOP emphasizes procedural programming and avoids the use of objects.
b. OOP allows for code reusability, modularity, and easier maintenance.
c. OOP is only applicable to high-level programming languages.
pg. 84
d. OOP does not support the concept of inheritance.
Answer: b. OOP allows for code reusability, modularity, and easier maintenance.
What is the output of the following code?
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
std::cout << i << " ";
}
a. 0 1 2 3 4 b. 0 1 2 3
c. 0 1 2 d. 0 1
Answer: c. 0 1 2
Which of the following statements about file handling in programming is true?
a. File handling is only applicable to text files and not binary files.
b. File handling allows for reading and writing data to and from files.
c. File handling can only be performed in low-level programming languages.
d. File handling is not supported in modern programming languages.
Answer: b. File handling allows for reading and writing data to and from files.
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
a. 1 2 3 4 5 b. 1 2 3 4
c. 0 0 0 0 0 d. Garbage values
Answer: a. 1 2 3 4 5
Which of the following statements about recursion in programming is true?
a. Recursion is a programming technique that is only applicable to mathematical problems.
b. Recursion is a process of calling a function within the same function.
c. Recursion can only be implemented in low-level programming languages.
pg. 85
d. Recursion is not supported in modern programming languages.
Answer: b. Recursion is a process of calling a function within the same function.
What is the output of the following code?
int x = 10;
if (x > 5) {
std::cout << "Hello";
} else {
std::cout << "World";
}
a. Hello b. World
c. Hello World d. No output is generated
Answer: a. Hello
Which of the following statements about data structures is true?
a. Data structures determine the physical layout of data in memory.
b. Data structures are only used in database management systems.
c. Data structures do not affect the performance and efficiency of algorithms.
d. Data structures are only applicable to high-level programming languages.
Answer: a. Data structures determine the physical layout of data in memory.
pg. 86