CS 2337 Test 1 Practice

studied byStudied by 13 people
5.0(1)
Get a hint
Hint

What does it mean that C is an expression-based language?

a. Operators and operands return values

b. Only functions can return values

c. Expressions are evaluated at compile time

d. Variables must be explicitly declared

1 / 86

flashcard set

Earn XP

Description and Tags

This practice is based off the description of the 84 questions on the test and should cover all of these topics relativally well although there is potentially some errors.

87 Terms

1

What does it mean that C is an expression-based language?

a. Operators and operands return values

b. Only functions can return values

c. Expressions are evaluated at compile time

d. Variables must be explicitly declared

a

New cards
2

In the following code, what will be the output if x is 5?

if (x = 5) { // code to execute }

a. The code inside the block will not execute

b. The code inside the block will always execute

c. A compilation error occurs

d. The condition is false

b

New cards
3

Which of the following lists the memory areas in order from high to low?

a. Stack, heap, initialized data, uninitialized data, text

b. Heap, stack, uninitialized data, initialized data, text

c. Stack, heap, uninitialized data, initialized data, text

d. Uninitialized data, initialized data, heap, stack, text

c

New cards
4

What is the lifetime of static variables in C++?

a. Until the program ends

b. Until they are explicitly deleted

c. Until they go out of scope

d. Until the function ends

a

New cards
5

In the following code, which variable is accessible outside the function?

void example() {

int localVar;

for (int i = 0; i < 5; i++) {

localVar = I;

}

}

a. localVar

b. I

c. Both localVar and i

d. None of the above

d

New cards
6

How can you modify an outside variable from within a function?

a. By using the address operator &

b. By returning the variable by value

c. By declaring it as static

d. By using global variables

a

New cards
7

What does the following C-string declaration represent?

char str[] = "Hello";

a. An array of characters terminated by a null character

b. A pointer to a string literal

c. A string with dynamic memory allocation

d. A character array with no termination

a

New cards
8

In the following code, what is the purpose of \0?

char str[] = "Hello";

a. It marks the end of the string

b. It is the first character of the string

c. It indicates the size of the array

d. It is used for memory allocation

a

New cards
9

Which of the following correctly shows the relationship between type sizes in C++?

a. short ≥ int ≥ long

b. long ≤ int ≤ short

c. short ≤ int ≤ long

d. int ≤ short ≤ long

c

New cards
10

What is the purpose of the scope resolution operator (::) in the following example?

MyClass::crazyFunc() {
return 🦍;

}

a. To define member functions outside of a class

b. To declare variables in the global scope

c. To access private members of a class

d. To create a new namespace

a

New cards
11

What is the purpose of if, else if, and else in C++?

a. To perform arithmetic operations

b. To control the flow of the program based on conditions

c. To define functions

d. To declare variables

b

New cards
12

What happens if a switch statement does not include break?

a. It throws an error at runtime

b. It skips to the next case without executing the current one

c. It goes through all cases starting from the chosen case

d. It exits the switch statement immediately

c

New cards
13

What is the result of the following condition if x is 5?

if (x = 5) { // code to execute }

a. The condition is false

b. The condition is always true

c. A compilation error occurs

d. The code inside the block will not execute

b

New cards
14

What will be the result of the following condition if x is 0?

if (x = 0) { // code to execute }

a. The condition is true

b. The condition is false

c. A compilation error occurs

d. The code inside the block will not execute

b

New cards
15

Are variable names in C++ case sensitive?

a. Yes, variable names are case sensitive

b. No, variable names are not case sensitive

c. Only keywords are case sensitive

d. Only function names are case sensitive

a

New cards
16

According to De Morgan's rules, which of the following is correct?

a. !(a && b) = (!a && !b)

b. !(a || b) = (!a || !b)

c. !(a && b) = (!a || !b)

d. !(a || b) = (!a && b)

c

New cards
17

What is the difference between parameters and arguments in a function?

a. Parameters are values sent to the function, and arguments are in the function definition

b. Parameters are in the function definition, and arguments are sent to parameters

c. There is no difference; they are the same

d. Parameters can be modified; arguments cannot

b

New cards
18

New cards
19

What does short-circuiting mean in conditionals?

a. Both conditions are always evaluated

b. If the first condition is false in an && expression, the second condition is evaluated

c. If the first condition is true in an || expression, the second condition is skipped

d. Only the first condition is evaluated regardless of the outcome

c

New cards
20

In the following code, what will happen if ptr is nullptr?

int value = *ptr;

a. The value of ptr is printed

b. A segmentation fault occurs

c. The code will execute normally

d. A null pointer exception is caught

b

New cards
21

What can happen when accessing an uninitialized variable?

a. The program will crash

b. It will always return zero

c. It may contain garbage values leading to undefined behavior

d. It will be automatically initialized by the compiler

c

New cards
22

What does the following code example demonstrate?

int arr[5];

arr[10] = 100;

a. Proper array indexing

b. Array index out of bounds error

c. Assigning a value to a valid index

d. Initializing the array

b

New cards
23

In the following code, what is the issue?

char* str = "Hello";

str[0] = 'h';

a. It modifies the string literal, which is undefined behavior

b. It is a valid operation

c. It will throw a compile-time error

d. The string will be successfully modified

a

New cards
24

What happens when data is accessed by multiple threads without synchronization?

a. The program will always run correctly

b. It can lead to data corruption or race conditions

c. It will automatically synchronize

d. The program will terminate

b

New cards
25

What is the result of returning a local variable by reference in the following function?

int& localVar() {

int x = 10;

return x;

}

a. The function returns a valid reference to x

b. It causes undefined behavior

c. The reference is valid until the function scope ends

d. The reference can be modified safely

b

New cards
26

What is integer overflow?

a. When an integer variable is reset to zero

b. When an operation results in a value that exceeds the maximum limit for the data type

c. When an integer is divided by zero

d. When an integer variable is not initialized

b

New cards
27

Can a function have more than one return statement?

a. No, only one return statement is allowed

b. Yes, but only one can execute

c. Yes, all return statements will execute

d. No, it will cause a compile-time error

b

New cards
28

Which of the following is true about constructors in C++?

a. They have a return type

b. They must have a different name from the class

c. They can be overloaded

d. They cannot access private members of the class

c

New cards
29

What does const do when applied to a function parameter?

a. It allows the parameter to be changed within the function

b. It prevents the parameter from being modified in the function

c. It makes the function return a constant value

d. It indicates the function does not modify any class members

b

New cards
30

In the following code, what is the effect of the const keyword on the member function?

int getValue() const { // function body }

a. The function can modify member variables

b. The function cannot modify any member variables

c. The function has no return type

d. The function can be called only from const objects

b

New cards
31

What happens when a reference is passed in a function parameter?

a. The function can change the value of the argument passed

b. The argument is passed by value

c. The function cannot modify the variable

d. It results in a compilation error

a

New cards
32

In the following function declaration, what can be modified?

int& function() {

static int x = 5;

return x;

}

a. The function returns a reference that can directly modify x

b. The function cannot modify x

c. The function returns a copy of x

d. The function will not compile

a

New cards
33

How should objects of a class be declared properly?

a. objectName className;

b. className objectName(params);

c. className objectName();

d. className::objectName;

b

New cards
34

What happens if you declare an object like this?

className objectName();

a. It creates an object named objectName

b. It declares a function named objectName that returns a className

c. It will throw a compilation error

d. It creates an instance of the class

b

New cards
35

How do you call a method from an object using the dot accessor?

myCar.run();

a. The method will not execute

b. This is the correct way to call the method

c. You must use the -> operator

d. It will cause a runtime error

b

New cards
36

How do you define a template function in C++?

template <class T>

int add() { // function body }

a. It can accept any data type

b. It only accepts integer data types

c. It must have a return type of void

d. It cannot be overloaded

a

New cards
37

In the following code, how would you call the function?

int add(int a, int b) {return a + b}

a. add(a, b);

b. add(1, “c“);

c. add(1, 2);

d. add(a, b);

c

New cards
38

In the following class declaration, what is the purpose of intBug?

template <class T>

class Bug {};

Bug<int> intBug(21);

a. It defines a function template

b. It creates an instance of Bug with int as the template parameter

c. It is a template specialization

d. It will throw a compilation error

b

New cards
39

How can you identify if a function is defined inline?

inline int add() { // function body }

a. The function must have a return type

b. The function definition is included in the header file

c. The function cannot be called from other files

d. It is declared in the implementation file

b

New cards
40

Which is faster: inline or non-inline functions?

a. Non-inline functions are faster

b. Inline functions are faster

c. Both have the same speed

d. It depends on the implementation

b

New cards
41

In the following constructor definition, how are arguments passed to the base class?

class Derived : public Base {

public:

Derived(int x, int y) : BaseClassName(x) {}

};

a. By using the constructor initializer list

b. By calling the base class constructor within the constructor body

c. By using the :: operator

d. Arguments cannot be passed to the base class constructor

a

New cards
42

What is the term for multiple classes derived from the same base class?

a. Child classes

b. Sibling classes

c. Parent classes

d. Cousin classes

b

New cards
43

What does it mean for a class to be a child of a base class?

a. It cannot access any member functions

b. It inherits attributes and methods from the base class

c. It has no relationship to the base class

d. It must be a virtual class

b

New cards
44

In the context of inheritance, what describes multilevel inheritance?

a. A class cannot inherit from another class

b. A derived class can become a base class for another derived class

c. All classes must be at the same level

d. Only one level of inheritance is allowed

b

New cards
45

What does the following declaration indicate about access specifiers?

class Derived : protected Base1 {};

a. All members are publicly accessible

b. Members of Base1 are private to Derived

c. Non-private members of Base1 are accessible to Derived and its subclasses

d. Members of Base1 are private to all classes

c

New cards
46

What is static binding in C++?

a. It occurs when a function is defined as virtual

b. It occurs when function calls are resolved at compile time

c. It occurs when all functions are in-line

d. It occurs when all class members are private

b

New cards
47

How are non-virtual functions treated during inheritance?

a. They are resolved at runtime

b. They are resolved at compile time

c. They cannot be overridden

d. They are treated as virtual by default

b

New cards
48

What does it mean when a derived class function has the same name as a base class function?

a. The base class function hidden unless the base function is marked final

b. It cannot compile

c. It will always call the base class function

d. It will throw a runtime error

a

New cards
49

In the context of polymorphism, what happens when a base class pointer points to a derived class object?

a. Only the base class functions can be called unless they are virtual and overridden by the derived class

b. The derived class functions will be called unless they have the same declarations as base functions

c. The program will crash

d. It leads to a compilation error

a

New cards
50

In the following example, what is the significance of the virtual keyword?

class Base {

public:

virtual void show() { // Base implementation }

};

a. It allows the function to be called without creating an object

b. It ensures the function can be overridden in derived classes

c. It restricts access to the function

d. It marks the function as static

b

New cards
51

What happens if a derived class attempts to override a final member function in the base class?

a. It compiles successfully but does not execute

b. It results in a compilation error

c. It will call the base class function instead

d. The derived class function will execute

b

New cards
52

Can a derived class hide a final member function from the base class?

a. Yes, it can hide the function but not override it

b. No, it cannot hide or override it - compile error

c. Yes, it can override the function if it is also declared final

d. No, it will throw a runtime error

b

New cards
53

Which of the following correctly defines the three access specifiers in C++?

a. Public: only visible to the class itself; Protected: visible to the class and derived classes; Private: anywhere the object is visible

b. Public: anywhere the object is visible; Protected: only visible to the class itself; Private: visible to the class and derived classes

c. Public: anywhere the object is visible; Protected: visible to the class and derived classes; Private: only visible to the class itself

d. Public: only visible within the same file; Protected: anywhere the object is visible; Private: only visible to the class and derived classes

c

New cards
54

What is inheritance in object-oriented programming (OOP)?

a. A way to encapsulate data

b. A mechanism for creating classes that derive attributes and methods from other classes

c. A method of polymorphism

d. A way to hide data

b

New cards
55

Which access specifier in a base class does not allow access from derived classes?

a. Public

b. Protected

c. Private

d. Static

c

New cards
56

In the context of constructor calls during inheritance, which is called first?

a. The derived class constructor

b. The base class constructor

c. Both are called simultaneously

d. It depends on the access specifier

b

New cards
57

What is the default behavior of calling a base class constructor in a derived class if not specified?

Derived(int x) : Base() {}

a. The derived class constructor is skipped

b. The default base constructor is called

c. The program will crash

d. A compilation error occurs

b

New cards
58

What is the definition of polymorphism in OOP?

a. The ability of an object to take on multiple forms

b. The restriction of a class to a single instance

c. The inheritance of properties from a single base class

d. The encapsulation of data within a class

a

New cards
59

In C++, how can a class be derived from multiple base classes?

class Derived : public Base1, public Base2 {};

a. This is not allowed in C++

b. It is called multiple inheritance and allows the derived class to inherit methods and attributes from both base classes

c. It causes a compilation error

d. Only one base class can be specified

b

New cards
60

How do you declare a nested class within a subclass in C++?

class Outer {

class Inner { // Inner class members };

};

a. It can be declared anywhere in the Outer class

b. It must be declared after all member functions of the Outer class

c. It cannot access private members of the Outer class

d. It must have a different name from the Outer class

a

New cards
61

What is true about a nested class?

a. It cannot access private members of the outer class

b. It can access private members of the outer class

c. It must be declared as public

d. It can only have static members

b

New cards
62

In the following code, what does Outer::Inner inner; represent?

class Outer {

class Inner {};

};

a. It declares an instance of the Outer class

b. It declares an instance of the Inner class defined within Outer

c. It will cause a compilation error

d. It declares a nested class without defining it

b

New cards
63

What is the purpose of the override keyword in the following function?

int add() override {}

a. It prevents the function from being overridden in derived classes

b. It indicates that the function overrides a virtual function in the base class

c. It allows the function to be called without creating an object

d. It defines a new member function

b

New cards
64

What does it mean to refactor a function?

a. To remove the function entirely

b. To restructure or improve code without affecting its behavior

c. To change the return type of the function

d. To overload the function with different parameters

b

New cards
65

What is encapsulation in object-oriented programming?

a. Hiding the implementation details of a class

b. The ability to inherit from multiple base classes

c. The process of overriding functions

d. Making all class members public

a

New cards
66

How does redefining a public member function work?

a. Hiding: Hides the base class function

b. Overriding: Allows derived classes to call the base function

c. Hiding: Makes the base class function private

d. Overriding: Requires the base function to be static

a

New cards
67

What is the difference between static and virtual functions?

a. Static functions can be overridden, while virtual functions cannot

b. Virtual functions cannot be accessed without an object, while static functions can

c. Static functions operate on specific instances, while virtual functions do not

b

New cards
68

In the following example, what does class::staticFunc() demonstrate?

class MyClass {

public:

static void staticFunc() { // function body }

};

a. The function must be called on an instance of the class

b. The function can be accessed without an object of the class

c. The function can be overridden by derived classes

d. The function cannot return a value

b

New cards
69

What are exceptions used for in C++?

a. To manage memory

b. To handle errors and exceptional situations

c. To define new data types

d. To create global variables

b

New cards
70

What happens if an exception is never caught?

a. The program continues execution

b. The program terminates and unwinds the call stack with an exit code

c. The program enters an infinite loop

d. The program resets all variables

b

New cards
71

Must a throw statement be inside a try block?

a. Yes, it must always be in a try block

b. No, but if not caught, it will terminate the program

c. No, it can only be called from main

d. Yes, it must be in the catch block

b

New cards
72

What exception is thrown when a new operation fails to allocate memory?

a. std::runtime_error

b. std::out_of_memory

c. std::bad_alloc

d. std::exception

c

New cards
73

What does the following code represent?

catch (...) { }

a. It catches only runtime exceptions

b. It catches all exceptions regardless of type

c. It will terminate the program

d. It is a compile-time error

b

New cards
74

Can a try block have more than one throw point?

a. No, it can only have one throw point

b. Yes, errors can occur from anywhere within the try block

c. Yes, but only if all throws are of the same type

d. No, it will result in a compilation error

b

New cards
75

Can a program contain both a regular version and a template version of a function?

a. No, only one version is allowed

b. Yes, this is called function overloading, and the regular version is preferred by the compiler

c. Yes, but only if the template version has different parameter types

d. No, it will lead to a runtime error

b

New cards
76

What types can a function template accept?

a. Only primitive data types

b. Only built-in types

c. Any parameterized types, including class types

d. Only user-defined types

c

New cards
77

Can a template have multiple parameterized types?

a. No, templates can only accept one type

b. Yes, in this case they can operate on multiple types

c. Yes, but only if the types are the same

d. No, it leads to compilation errors

b

New cards
78

What is the correct syntax for defining a template with two parameterized types?

a. template <class 1, class 2>

b. template <T1, T2>

c. template <class T1>

d. template <class T1, class T2>

d

New cards
79

Can code exist between a try block and its corresponding catch block?

a. No, it will cause a compile-time error

b. Yes, but it will not execute if an exception occurs

c. Yes, but only if the code is inside the catch block

d. No, it is not allowed in C++

b

New cards
80

Does a function template reserve memory when declared?

a. Yes, it reserves memory for all types

b. No, it only reserves memory when instantiated with specific types

c. Yes, it reserves memory at compile time

d. No, it does not reserve any memory

b

New cards
81

What happens when a throw statement is used with no catch?

a. The program continues running

b. The program unwinds and terminates

c. The exception is ignored

d. A segmentation fault occurs

b

New cards
82

In a function template, what does a parameterized type refer to?

a. The return type of the function

b. The specific data types of the arguments sent to the template function

c. The types of local variables in the function

d. The type of exceptions thrown by the function

b

New cards
83

What do template parameters allow in C++?

a. The creation of fixed data types

b. The writing of generic reusable code for multiple data types

c. The restriction of functions to single data types

d. The simplification of complex types

b

New cards
84

Do recursive algorithms end when the base case is encountered?

a. Yes, this is the definition of a base case

b. No, they continue until all calls are resolved

c. It depends on the algorithm

d. Base cases are optional in recursion

a

New cards
85

Can an iterative algorithm be implemented using recursion?

a. No, recursion and iteration are mutually exclusive

b. Yes, but it is less efficient than the iterative version

c. Yes, in general, iterative algorithms can be made with recursion

d. No, recursion cannot replace iteration

c

New cards
86

In general, which is faster: iterative algorithms or recursive algorithms?

a. Iterative algorithms are usually faster

b. Recursive algorithms are always faster

c. They have the same performance

d. It depends on the algorithm used

a

New cards
87
New cards

Explore top notes

note Note
studied byStudied by 10 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 47 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 14 people
Updated ... ago
4.0 Stars(1)
note Note
studied byStudied by 32 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 18 people
Updated ... ago
5.0 Stars(2)
note Note
studied byStudied by 9 people
Updated ... ago
5.0 Stars(2)
note Note
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 278 people
Updated ... ago
5.0 Stars(1)

Explore top flashcards

flashcards Flashcard21 terms
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard20 terms
studied byStudied by 23 people
Updated ... ago
4.8 Stars(8)
flashcards Flashcard21 terms
studied byStudied by 166 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard50 terms
studied byStudied by 14 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard33 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard21 terms
studied byStudied by 7 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard21 terms
studied byStudied by 7 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard40 terms
studied byStudied by 146 people
Updated ... ago
5.0 Stars(2)