Midterm Pratice Flashcards

1
Q

Which of the following statements allows us to simply type “cout” instead of “std::cout” everytime we wish to use the cout variable?

A

using namespace std;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Using the same string variable called ‘name’ from the previous question, display the name to the console in a single line of code followed by an end of line.

Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.

A

cout &laquo_space;name &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Assume there is a string called ‘name’ that has already been defined. In a single line of code, read the user’s ENTIRE name into the string from the console.

Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.

A

getline(cin,name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Assume that there is a string variable named “title” with some phrase of length 10 or more. Display the 7th character to the console and nothing else.

A

cout &laquo_space;title[7];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following also known as an instance of a class?

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A struct is declared as follows:

struct NameType
{
    char first[10];
    char last[20];
    int age;
}
The first letter in the first name is stored in location 1000. Each letter takes one location and an integer takes 2 locations.  What is the address of last[2]?
A

1,012 (with margin: 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when a class with parameterized constructors and having no default constructor is used in a program and we create an object that needs a zero-argument constructor?

A

Compile-time error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
#include 
using namespace std; 
class Test 
{ 
public: 
  Test(); 
}; 

Test::Test() {
cout &laquo_space;“Constructor Called” &laquo_space;endl;
}

int main() 
{ 
    cout << "Start" << endl; 
    Test t1(); 
    cout << "End" << endl; 
    return 0; 
}
A

Start

End

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
#include
using namespace std;
class Test
{
public:
  Test();
};

Test::Test() {
cout &laquo_space;” Constructor Called. “;
}

void fun() {
  static Test t1;
}
int main() {
    cout << " Before fun() called. ";
    fun();
    fun();
    cout << " After fun() called. ";  
    return 0;
}
A

Before fun() called. Constructor Called. After fun() called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
#include
using namespace std;
class Point {
    Point() { cout << "Constructor called"; }
};
int main()
{
   Point t1;
   return 0;
}
A

Compiler Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Using the same double pointer, gpaPtr, from the previous question, delete its memory.

A

delete gpaPtr;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
int *p;
int i, k;
i = 142;
k = i;
p = &i;
Which of the following statements changes the value of i to 143 ?
A

*p = 143;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In a function, passing a parameter by value does not necessarily protect the contents of the caller’s data structures from being affected by the function under which of the following conditions?

A

The value parameters are pointer variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Which of the following also known as an instance of a class?
  Member Variables 
  Friend Functions 
  Object 
  Member Functions
A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Assume that a class called Weapon exists that has a default constructor. Create a pointer to a Weapon (named myWeapon) and the Weapon associated with it.

A

Weapon* myWeapon = new Weapon();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Assume that a pointer to a Weapon object exists named “myWeapon”. Call the Fire method on myWeapon. Assume the Fire method does not have any parameters. Ignore any return values from the Fire method.

A

myWeapon->Fire();

17
Q

A deep copy copies one class object to another without copying any pointed-to data.

A

false

18
Q

A shallow copy copies one class object to another without copying any pointed-to data.

A

true

19
Q

A class should provide a copy constructor in which of the following cases?

A

At least one data member is a pointer.

20
Q

What is the right way to declare a copy constructor of a class if the name of the class is MyClass?

A

MyClass (constant MyClass &arg)

21
Q

A copy constructor is automatically built into any class that needs it.

A

false

22
Q

The capacity method of the vector class represents the number of items stored in the vector.

A

false

23
Q

Using the same dynamic array called ‘names’ from the previous question, assume that we have another pointer called ‘item’ that points to the some item in the array. Move ‘item’ to point to the NEXT item in the array.

Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.

A

item++;

24
Q

Assume that there is an integer variable called ‘size’ and that we have already asked the user to provide a value for the size of a dynamic array. Assume that we have created a dynamic array of strings called ‘names’ that has space for ‘size’ number of strings.

Delete the ‘names’ array.

Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.

A

delete[] names;

25
Q

In a function, passing a parameter by value does not necessarily protect the contents of the caller’s data structures from being affected by the function under which of the following conditions?
The function is recursive.
The value parameters are arrays
The value parameters are pointer variables
The value parameters are integers.
Value parameters always protect the caller’s data

A

The value parameters are pointer variables

26
Q

Dynamic binding is accomplished using the virtual keyword on a method in the base or parent class.

A

true

27
Q
#include 
using namespace std; 
class Base1 { 
public: 
	Base1() 
	{ cout << " Base1's constructor called" << endl; } 
}; 
class Base2 { 
public: 
	Base2() 
	{ cout << "Base2's constructor called" << endl; } 
}; 
class Derived: public Base1, public Base2 { 
public: 
	Derived() 
	{ cout << "Derived's constructor called" << endl; } 
}; 
int main() 
{ 
Derived d; 
return 0; 
}
A

Base1’s constructor called

Base2’s constructor called

Derived’s constructor called

29
Q

In C++, if class X is a base class of class Y, then Y cannot directly access X’s private data.

A

true

30
Q

Virtual functions are the C++ mechanism for dynamic binding of operations to objects.

A

true

33
Q

Neither client code (main, etc.) nor derived class code can directly access the private members of the base class.

A

true

34
Q

Assume that a linked list is defined with only a pointer to the head (first item) in the list. Which of the following represents the efficiency of adding a new item to the back of the list?

A

O(n)

35
Q

The next item in a linked list can always be found by accessing the next physical location in memory.

A

false

36
Q

Reading components into an initially empty list is faster if the list is represented directly as an array rather than a linked list.

A

true

37
Q

If currPtr points to a node in a dynamic linked list, the operation currPtr++ advances to the next node in the list.

A

false

38
Q

Select all of the following that are true about Constructors.
They cannot be virtual.
They are automatically called by new operator.
They cannot be private.

A

They cannot be virtual.

They are automatically called by new operator.

41
Q

Which of the following statements is true (in C++)?
For a function to be virtual, its object parameter(s) must be passed by reference to the derived function.
For a function to be virtual in a derived class, it must be virtual in the base class.
All of these statements are true. [except for “None of these…”]
If a virtual function has its parameters passed by reference, the actual parameter, not the formal parameter determines which function to use.
None of these statements is true.

A

All of these statements are true. [except for “None of these…”]

46
Q

Assume that we have a function Foo() defined as follows (what is does is irrelevant):

void Foo(T a) { /* code */ }
In main, Foo is invoked as follows:

Foo(b);
Which of the following are true about the relationship between the type of the formal

  None of these statements is true.  
  All of these statements are true.  
  Actual parameter may be an object from a derived class of the formal parameter.  
  Actual and corresponding formal parameter must be identical.  
  The formal parameter may be an object from a derived class of the actual parameter.
A

Actual parameter may be an object from a derived class of the formal parameter.

47
Q
Which of the following statements is correct?
  Pointer to derived class cannot be created. 
  Derived class pointer cannot point to base class. 
  Base class pointer cannot point to derived class. 
  Pointer to base class cannot be created.
A

Derived class pointer cannot point to base class.