Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views7 pages

C++ Programming Exams Questions

Uploaded by

OpizaroTech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

C++ Programming Exams Questions

Uploaded by

OpizaroTech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ATTEMPTALLQUESTIONS

TOTAL MARKS: 40

theouter loop, how many times oes


of
for every single iteration
1. When working with nested loops,
the inner loop run?
Once
b. Zero times

E. Completely
false
d. Only ifthe outer loops condition is

2. What are multi-dimensional arrays primarily used for in real-world applications?


Storing a single value
b. Representing a sequence of unrelated data
subjects, or handling tables of
Representing matrices, storing marks for multiple students
in
e
data.

d. Dynamically resizing collections of different data types

3. What does sizeof) operator return when used on an entire array (e.g., sizeof(myNumbers);) ?
a. The number of elements in the array.
b. The size of the first element in the array.
C. The total size of the array in bytes.
d. The maximum possible value that can be stored in the array.

4. If youdeclare an array without specifying its size, like string cars]=("Volvo","BMW", "Ford"};,
what will the compiler do?
a. It will generate a compilation error.

b. It will set the size to a default large value.


C. It will determine the size based on the number ofinserted values.
d. It will create a dynamicarray (vector).

5. What is the main problenm with omitting the array size in its declaration if you intend to add more
elements later?

a. It makes the array non-indexable.


b. You will have to overwrite existing values to add future elements.
c. The compiler will automatically resize the array.
d. It causes a runtime error when accessing elements.

6. Which of the following is an exampleof a "library function" in C++?


a. calculateArea() (user-defined)
b. greet() (user-defined)
C. myMethod) (user-defined)
d. strlen)
7. What does the return type specify in a functions syntax return type function_name
(parameter list)?
a. The name of the function.
b. The list of variables
passed to the function.
c. The type of value the function will send
back.
d. The block of statements defining the functions task.

8. What happens when a structure is passed to a function using Call by Value?


a. The original structure in main() is directly modified.
b. A copy of the structure is made and sent to the function.
C. The memory address ofthe original structure is passed.
d. The function works directly on the
original structures memory.

9. When passing a structure to a function using Call by Reference, what symbol is used in the
functions parameter list to indicate this?
a. * (asterisk)
b. .(dot)
C.
::(scope resolution operator)
d. & (ampersand)

10. What is the primary characteristic of "Function Overloading" in C+-+?


a. Functions can call themselves recursively.
b. Functions can only be defined once per program.
c. Multiple functions can have the same name but differ in parameter count, type, or order.
d. Functions can only have one return type.

11.What happens you try to overload a function by only changing its


if return type?
a. The compiler will automatically select the correct version.
b. It is a valid form of function overloading.
C. It cannot be done and will result in an error.
d. It creates an inline function.

12. What is the fundamentalrequirement for a recursive function to prevent an infinite loop?
a. It must call itself a fixed number oftimes.
b. It must have a break statement.
c. It must have a base condition to stop the recursion.

d. It mnust use goto statements.

13. In C++, where are default values for function parameters typically assigned?
a. In the function definition only.
b. In the function callonly.
C. In the function declaration.

d. In a global variable.

14. What is the purpose of using the inline keyword for a function?
a. It forces the function to be compiled as a separate module.
b. It makes the functions address accessible via pointers.

c. The compiler attempts to expand the function inline, reducing function call overhead.
d. It makes the function behave like a class method.

EXAME UFFICER
Engineering
of
Facuitly University
Technical
Koforidua Koforidua

3 Data.
the following best describes the role of "Classes" in OOP2
15. Which of

a. They are instances of objects.


b. They describe the behaviors of objects.
C. They serve as blueprints or templates for creating objects.

d. They hold the actual data relevant to an object.

16. What is the term for variables declared within a class that hold data relevant to an object?
a. Methods
b. Objects
e. Attributes
d. Functions

17. Which of the following is a primary reason for using classes in C++?
a. To avoid using functions.
b. To make all data public.
C. To organize and maintain code, and to model real-world entities.
d. To prevent code reuse.

18.In a class definition, what is the role of the public keyword?


a. It restricts access to class members.
b. It makes memnbers non-modifiable.
C. It specifies that members are accessible from outside the class.
d. Itdefines the class constructor.

19. How do you create an object from a class (e.g., Car class)?
a.
Car.myObject;
b. new Car();
c. Car myObject;
d. object Car;

20.When defining a class method outside the class definition, what operator is
class the used to specify which
method belongs to?
a (dotoperator)
b. -> (arrowoperator)
c. ::(scoperesolution operator)
d. )(parentheses)
21.What is a key characteristic of a constructor in
C++?
a. It must be explicitly called by the
b.
programmer.
It is used to destroy objects.
e. It is automatically called when an object of a class
d.
is created.
It returns a value ofthe class type.

22. What is one of the


mandatory rules for defining a constructor?
It must have
a void return
type.
b. It must have the same namneas the
class.
C. It must take at least one
parameter.
d. It is usually declared private.
23.What is the primarybenefit of constructor overloading?

a. ltallows constructors to return different data types.


different initializations.
Itprovides flexibility when creating objects by allowing
b.
C. It enables constructors to be inherited.
d. It removes the need for default arguments.

members to be accessed only within the class itself

24.Which of the following access Specifiers allows


and by child classes?
a. public
b. private
C. protected
d. default

ifnone is explicitly specified?


default access specifier for members ofa class
25.What is the
a. public
b. protected
private
d. friend

26. What is the main objective of "Encapsulation" in 0OP?


a. To make all data members visible to external code.
b. Toallow direct manipulation ofprivate attributes.
private and providing public
e. To hide "sensitive" data from users by making class attributes
methods for access.
d. Toachieve function overloading.

from outside the class, adhering to encapsulation


27.How can you access private members ofa class
principles?
a. By directly referencing the private member.
b. By casting the object to a public type.

e. By providing public "get" and "set" methods.

d. Using a global variable.

28.What is a "friend function" in C++?


a. A member function ofanother class.
b A function that can only access public members of a class.
A non-member function that is granted special permission to access a classs private data.

d. A function that is automatically called when a class object is created.

29. What is the primary benefit of "Inheritance" in C++?

.
b.

d.
It prevents

It
polymorphism.
allows direct access to private members of any class.
Itenables code reusability by allowing one class to acquire attributes and methods from
another.
It forces allclasses to have the same structure.

30. In the context of inheritance, what is a "derived class" also known as?

Parent class
b. Base class
c. Child class OFFICER
EXAMS
ot Eng!nB€rins
d. Superclass University
Feculty
Technica

S
Koforidua Koforidua

ate.
simultaneously?
31. Which type of inheritance allows a class to inherit from multiple
base classes
a. Single Inheritance
b. Multilevel Inheritance
c. Hierarchical Inheritance

d. Multiple Inheritance

32. What does "Polymorphism"literally mean in C++ OOP?


a. Single form
b. Multiple inheritance
C. Many forms
d. Hidden forms

33. Ifa base class Animal has a method makeSound(),and derived classes like Pig and Dog override
this method toproduce different sounds, this is an example ofwhich 0OP principle?
a. Encapsulation
b. Abstraction
C. Inheritance
d. Polymorphism

34.What is the purpose of a "virtual function" in a base class?


To prevent it from being overridden in derived classes.
b. To make it accessible only tothe base class.

e. To allow it to be overidden in derived classes, enabling dynamic dispatch based on the


actual object type.
d. Toenforce static binding at compile time.

35. When defining an overriding virtual function in a derived class, what is the recommended (though
optional) keyword to use for clarity and error checking?
a. virtual

b. new
e. override
d. final

36. What is the -> operator used for in C++?


a. To access members of an object directly.
b. To declare a pointer.

C. To access members (functions or variables) through a pointer.


d. To define a new data type.

37.What is the primary benefit of using C++ "Templates"?


a. They strictly define the data type that can be used.
b. They only work with built-in data types.

c. They allow writing generic functions or classes that work with different data types, avoiding
code repetition.
d. They eliminate the need for function parameters.

38. What does the time() function return?

a. A string representing the current date.


b. A struct tm representing datetime components.
C. A timestamp representing the current date and time.
d. The systemsuptime in seconds.
39. Inthe struct tm datetime structure, how is January represented for the tm mon member?
a. 1

b. 12
e. 0
d. "January"

40. In the structtm datetime structure, how is the year 2024 represented for the tm year member?
a. 2024
b. 1900
c. 124 (since 1900)
d. As a string "2024"

UrFICER
EXAME
ofEngineering
Universlty
Facuity Technical

Koforidua
Koforidua

Date
Question One (1) and one (1)other question.
SECTION B: Answer
20MARKS

output it will producewhen executed:


I. Carefully study the following C+t program and write down the exact

#include <iostream>

using namespacestd;

class Car{
public:
string brand;
int year;

void display() {
cout << brand <<" "<<year << endl;

int main(0 {
Car cars[10] ={
{"Toyota", 2010}, ("BMW", 2011),("Honda",2012}, {"Ford", 2013},
("Kia", 2014), ("Nissan", 2015}, {"Hyundai", 2016} ("Audi",2017),,
{"Mazda'", 2018},{"Mercedes", 2019}

for (int i=0; i< 10; it+) {

cars[i].display():

return 0;

2. Write program that uses a structure with a nested structure to


a store and print a student's name, ID, and date
of birth (day, month, year). 10 Marks
Outer and inner structure definitions: 4 marks
11. Input and assignment of values: 3 marks
iii. Output format: 2 marks
1V. Correct use ofdot operator: 1mark

3. Write a program to declare a 2D array to store marks of 3 students in 4 subjects. Calculate and display the
total and averagefor each student. 10 Marks
i
Declaration and initialization of 2D array: 3 marks
Loop for total and averagecal culation:4 marks
iii. Output formatting: 2 marks
iv.
Correct use of indices: 1 mark

You might also like