ECE Exam Notes

Q1: Array Prototypes

Question: Which of the following is NOT a correct prototype for an array passed as a parameter?

Options:

A. void getExams (int [][3]5);

B. void getExams (int [][COLS], int);

C. void getExams (int [ROWS][], int);

D. void getExams (int [][COLS]);

Answer: C. void getExams (int [ROWS][], int); (because you cannot leave the size of the second dimension of a multi-dimensional array undefined in a function prototype in C++).

Q2: Function Analysis

Question: What does the following function do?
void ABC (string str, int XYZ []){
for (int i = 0; i < str.length(); i++) {
if (isalpha(str[i])){
char ch = tolower(str[i]);
XYZ[ch - 'a']++;
}
}
}

Options:

A. This function will not work.

B. This function computes the number of lower-case characters (English letters) for a string.

C. This function computes the number of occurrences for each lower-case character (English letter) in a string.

D. This function computes the number of occurrences for each character (without caring lower or upper case) in a string.

Answer: C. This function computes the number of occurrences for each lower-case character (English letter) in a string.

Q3: C-String Array Definition

Question: Which of the following lines of code defines an array of C-strings that will hold 49 characters?

Options:

A. char [49];

B. char str[50];

C. char[50] str;

D. character str[50];

E. None of the above

Answer: B. char str[50]; (C-strings require an extra character for the null terminator, so an array of size 50 can hold a string of 49 characters.)

Q4: cin Behavior

Question: Given the following code, if Hello there are typed using the keyboard, what is the value stored in string1?

char string1[20], string2[ ]= "Good morning";
cout << "Enter a string: "; cin >> string1;

Options:

A. Hello

B. Hello there

C. There

D. None of the above

Answer: A. Hello (cin stops input at whitespace, so only "Hello" is stored in string1).

Q5: Function Parameters

Question: What happens if you pass a non-const reference to a function?

Options:

A. The function can modify the argument passed.

B. The function cannot modify the argument passed.

C. The reference will not work.

D. The reference will cause a compilation error.

Answer: A. The function can modify the argument passed.

Q6: Pointer Arithmetic

Question: What will the following code print?

int arr[5] = {1, 2, 3, 4, 5};
printf("%d", *(arr + 2));

Options:

A. 3

B. 4

C. 5

D. None of the above

Answer: A. 3 (because it points to the third element of the array which is indexed at 2).

Q7: Static Variables

Question: Which of the following is true about static variables in a function?

Options:

A. They are initialized every time the function is called.

B. They retain their value between function calls.

C. They cannot be used if the function is recursive.

D. They must be declared as global variables.

Answer: B. They retain their value between function calls.

Q8: Variable Scope

Question: What is the scope of a local variable in C++?

Options:

A. Entire program

B. Within the function it was declared

C. Only within the block it was declared

D. It does not have a scope

Answer: C. Only within the block it was declared.

Q9: Memory Management

Question: What is the output of the following code?

int *ptr = new int;
*ptr = 10;
cout << *ptr;
delete ptr;

Options:

A. 10

B. 0

C. Compilation error

D. Undefined behavior

Answer: A. 10 (the value 10 is assigned to the dynamically allocated memory).

Q10: Constructors

Question: What is the purpose of a constructor in a class?

Options:

A. To destroy an object

B. To initialize an object

C. To clone an object

D. To finalize an object

Answer: B. To initialize an object.

Q11: Inheritance

Question: What is the main benefit of using inheritance in OOP?

Options:

A. Code reusability

B. Increased code complexity

C. Slower performance

D. None of the above

Answer: A. Code reusability.

Q12: Abstract Classes

Question: What are abstract classes used for?

Options:

A. To instantiate objects

B. To define interfaces

C. To increase code complexity

D. None of the above

Answer: B. To define interfaces.

Q13: Operator Overloading

Question: Which operators cannot be overloaded in C++?

Options:

A. +, -, *

B. <<, >>

C. ::, ., ?

D. All of the above

Answer: C. ::, ., ? (these operators have a fixed meaning and cannot be overloaded).

Q14: Templates

Question: What is the purpose of templates in C++?

Options:

A. To send functions as arguments

B. To create generic classes and functions

C. To reduce code size

D. To increase processing speed

Answer: B. To create generic classes and functions.

Q15: Lambda Expressions

Question: What is a lambda expression?

Options:

A. A way to define anonymous functions

B. A method to overload operators

C. A type of class

D. A data structure

Answer: A. A way to define anonymous functions.

Q16: Exception Handling

Question: What keyword is used to throw an exception?

Options:

A. catch

B. finally

C. throw

D. except

Answer: C. throw.

Q17: Overriding Methods

Question: What is method overriding?

Options:

A. Creating a method with same name in the same class.

B. Creating a method in a derived class with same signature as in the base class.

C. Changing a method to static.

D. None of the above

Answer: B. Creating a method in a derived class with same signature as in the base class.

Q18: Virtual Functions

Question: What is the purpose of a virtual function?

Options:

A. To enforce compile-time binding

B. To allow runtime polymorphism

C. To improve performance

D. None of the above

Answer: B. To allow runtime polymorphism.

Q19: Multithreading

Question: Which of the following is a benefit of multithreading?

Options:

A. Increased performance

B. Simplicity in design

C. Improved security

D. None of the above

Answer: A. Increased performance.

Q20: File Handling

Question: How do you open a file for writing in C++?

Options:

A. fstream.open("file.txt", ios::out);

B. fstream.open("file.txt", ios::in);

C. fstream.open("file.txt");

D. None of the above

Answer: A. fstream.open("file.txt", ios::out);

Q21: Smart Pointers

Question: What are smart pointers?

Options:

A. Pointers that cannot be modified

B. Objects that manage dynamic memory automatically

C. Pointers used in matrix operations

D. None of the above

Answer: B. Objects that manage dynamic memory automatically.

Q22: Using Directives

Question: What is the purpose of using directives in C++?

Options:

A. To simplify code

B. To include libraries

C. To avoid prefixing with std::

D. All of the above

Answer: D. All of the above.

Q23: Recursion

Question: What is recursion?

Options:

A. A function that calls itself

B. A function that cannot call itself

C. A method to increase code complexity

D. None of the above

Answer: A. A function that calls itself.

Q24: Data Encapsulation

Question: What is data encapsulation?

Options:

A. Hiding data within a class

B. Sharing data between objects

C. Creating public data members

D. None of the above

Answer: A. Hiding data within a class.

Q25: Polymorphism

Question: What is polymorphism?

Options:

A. The ability of different classes to be treated as instances of the same class

B. The ability of a class to have multiple methods with the same name

C. Both A and B

D. None of the above

Answer: C. Both A and B.

Q26: FIFO Queue

Question: What does FIFO stand for in data structures?

Options:

A. First In, First Out

B. Fast In, Fast Out

C. First In, Fast Out

D. None of the above

Answer: A. First In, First Out.

Q27: LIFO Stack

Question: What does LIFO stand for?

Options:

A. Last In, First Out

B. Last In, Fast Out

C. First In, Last Out

D. None of the above

Answer: A. Last In, First Out.

Q28: Data Structures

Question: Which of the following is not a linear data structure?

Options:

A. Array

B. Linked List

C. Stack

D. Tree

Answer: D. Tree.

Q29: Searching Algorithms

Question: Which algorithm has the best average-case time complexity for searching through a sorted list?

Options:

A. Linear Search

B. Binary Search

C. Jump Search

D. Interpolation Search

Answer: B. Binary Search.

Q30: Sorting Algorithms

Question: Which of the following sorting algorithms has the worst-case time complexity of O(n^2)?

Options:

A. Quick Sort

B. Merge Sort

C. Bubble Sort

D. Heap Sort

Answer: C. Bubble Sort.

Q31: Hash Tables

Question: What is the primary purpose of a hash table?

Options:

A. To store data in a sorted manner

B. To provide fast access to data via a hash function

C. To allow for duplicate entries

D. None of the above

Answer: B. To provide fast access to data via a hash function.

Q32: Graph Traversal

Question: What is the difference between Depth First Search (DFS) and Breadth First Search (BFS)?

Options:

A. DFS uses a stack, while BFS uses a queue.

B. BFS is faster than DFS.

C. DFS can be more memory efficient than BFS.

D. Both A and C

Answer: D. Both A and C.

Q33: String Manipulation

Question: What function would you use to concatenate two C-strings?

Options:

A. strcopy()

B. strconcat()

C. strcat()

D. None of the above

Answer: C. strcat().

Q34: Scope Resolution

Question: What operator is used to define the scope resolution in C++?

Options:

A. ::

B. .

C. ->

D. []

Answer: A. ::