Programming Fundamentals 2 Midterm

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/76

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

77 Terms

1
New cards

Warnings can always be safely ignored.

 

True

False

False

2
New cards

What happens if you try to set an unsigned variable to a value less than the minimum possible value?

 

Nothing will happen.

The value will be set to 0.

The value will be set to the lowest possible value for that data type.

The value will underflow.

The value will underflow.

3
New cards

Warnings will stop your code from compiling and prevent you from building your program.

 

True

False

False

4
New cards

What value does the following variable have?

int someValue;

 

The minimum value for the data type

0

1

Impossible to know

The default value for all integers

Impossible to know

5
New cards

The first stage of the C++ build process is the _________________?

 

Predecessor

Preprocessor

Compiler

Linker

Comparator

Preprocessor

6
New cards

This is the final stage of the building process creates the executable.

 

Compiler

Linker

Preprocessor

Linker

7
New cards

A _________________ has everything a function has, except the implementation.

 

prototype

pointer

null function

definition

prototype

8
New cards

Sometimes, when building a program, we get a lot of compiler errors. Depending on what you're working on, it could be literally hundreds of them. Which of these errors should you fix first?

 

The longest one.

The first one.

The last one.

The one that looks easiest to fix.

The one that sounds the scariest.

The first one.

9
New cards

Which of the following is the appropriate way to print "Hello, world!" to the screen?

 

cout << "Hello, world!";

cout("Hello, world!");

cout "Hello, world!";

cout >> "Hello, world!";

cout << "Hello, world!";

10
New cards

What is the minimum value of any unsigned data type?

 

-1

It depends on the data type

There is no minimum

0

1

0

11
New cards

What is the return type for constructors?

 

int

No return type

void

char

No return type

12
New cards

Given a class called Widget, which of the following are valid constructors?

Mark all that apply

 

Widget(float x, double y, unsigned int num = 0);

int Widget();

void Widget(int x);

Widget(int x);

Widget();

Widget(float x, double y, unsigned int num = 0);

Widget(int x);

Widget();

13
New cards

While a class can only have one default constructor (more would be a compiler error), which of the following COULD be a default constructor for a class called Spaceship?

Mark all that apply

 

Spaceship(string name = "Spaceship", float weight = 0.0f);

Spaceship(float cost, string name, float weight);

Spaceship();

Spaceship(int serialNumber, bool movingSpaceship = true);

Spaceship(string name = "Spaceship", float weight = 0.0f);

Spaceship();

14
New cards

When writing classes, a header file is where you should write the:

 

prototype definitions

function definitions

class definition

class member function definitions

class definition

15
New cards

Which of the following declarations is an accessor?

 

int Student::GetGrade() { return grade; }

void SetName(string studentName);

void Print();

string GetName();

string GetName()

16
New cards

When writing classes, a source file is where you should write the:

 

class definition

variable definitions

class member function definitions

function definitions

class member function definitions

17
New cards

Given the following code:

// If the boss is at half health...
if (bossMonster.GetHitpoints() <= bossMonster.GetMaximumHitpoints() * 0.5f)
{
    // Look out player, you won't like him when he's angry!
    // TODO: Write really powerful boss behavior
}

What are the functions being called in the if statement considered?

 

Constructors

Mutators

Behaviors

Accessors

Destructors

Accessors

18
New cards

Grouping data and functionality together, otherwise known as ____________, is the core of why we use classes.

 

dynamic allocation

inheritance

polymorphism

encapsulation

encapsulation

19
New cards

Which of the following statements is true about public member functions of a class?

 

A class user need not understand how each public member function behaves.

A class user can call the functions to operate on the object.

A class user needs to know how the class' functions are implemented.

A class user can declare a variable of the class type to create a new function.

A class user can call the functions to operate on the object.

20
New cards

A class function could be used a number of times, by a number of different instances of that class.

In a class member function, how would you access the object that has called the function?

 

 

this.

that.

this->

that->

thisThingHere->

this->

21
New cards

Class variables are _______________ by default.

 

protected

public

private

private

22
New cards

In C++, objects must be constructed using the new keyword before they can be used.

 

True

False

False

23
New cards

What is this code called (collectively, all of it together)?

class Container
{
    vector<int> stuffHeldByContainer;
public:
    Container();
    void AddThingToContainer(int thingToAdd);
    int NumberOfElementStored();
    bool IsEmpty();
    void EmptyContainer(); 
};

 

class structure

class creation

class definition

class demarcation

class definition

24
New cards

Given the following code:

#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    void SetName(string studentName);
    void SetScore(int studentScore);
    string GetGrade();
private:
    string name;
    int score;
};

void Student::SetScore(int studentScore)
{
    score = studentScore;
}

string Student::GetGrade()
{
    string grade;
    if (score < 40)
        grade = "C";
    else if (score < 70)
        grade = "B";
    else
        grade = "A";

    return grade;
}

What will the output of this program be?

int main()
{
   Student stu1;
   stu1.SetScore(80);
   cout << stu1.GetGrade();
   return 0;
}

 

B

A

80

C

A

25
New cards

What is the output of this code?

What is the output of the following code?

#include <iostream>
using namespace std;

class Greet
{ 
public:
    Greet(); 
};

Greet::Greet()
{ 
    cout << "Hello"; 
}
  
int main()
{
    Greet greetings; 
    return 0; 
}

 

Hello Hello

Error: When compiling the code.

No Output.

Hello

Hello

26
New cards

Given the following function prototype:

void Hero::AttackEnemy(Enemy* target);

And the following objects declared in main():

Hero mainCharacter;
Enemy badGuy;

How would you call the function correctly?

 

mainCharacter.AttackEnemy(->badGuy);

mainCharacter.AttackEnemy(*badGuy);

mainCharacter.AttackEnemy(&badGuy);

mainCharacter->AttackEnemy(&badGuy);

mainCharacter.AttackEnemy(&badGuy);

27
New cards

What is the default value of a pointer?

 

NULL

nullptr

unknown, it is uninitialized

it depends on the type of pointer

unknown, it is uninitialized

28
New cards

In order to access a pointer's pointee (i.e. the thing it's pointing to), you must do what?

 

delete the pointer

reference the pointer

depoint the pointer

derive the pointer

dereference the pointer

dereference the pointer

29
New cards

For the following function, which line of code could complete the desired functionality. Assume the classes are properly defined and any variables written do what their names suggest.

// Purpose: Deal damage to the passed in target, according to the Hero's attack power.
void Hero::AttackEnemy(Enemy& target)
{
    // TODO: Write this function!
}

 

target.TakeDamage(this.attackPower);

target->TakeDamage(this.attackPower);

target.TakeDamage(this->attackPower);

target->TakeDamage(this->attackPower);

target.TakeDamage(this->attackPower);

30
New cards

References, once initialized (or "bound") to something, for all intents and purposes, ARE that thing.

 

True

False

True

31
New cards

Which of the following are true about pointers AND references?

 Mark all that apply.

They can be reassigned as needed

They MUST be initialized

They allow for data to be shared between functions without creating copies of the original

They can be set to nullptr to indicate they aren't pointing to anything

They allow for data to be shared between functions without creating copies of the original

32
New cards

Pointers provide what type of access to data?

 

direct access

reference access

indirect access

immediate access

memory access

indirect access

33
New cards

Which of the following causes a stack overflow error?

 

When your program tries to use more stack memory than is available

When the stack pointer is at the maximum value and it gets incremented, causing it to wrap around to the minimum value

When your program uses recursion

When you exceed the maximum number of variables in a function

When your program tries to use more stack memory than is available

34
New cards

Given the following code:

int count;
cin >> count;
float* values = new float[count];
float total = 0;

for (int i = 0; i < count; i++)
 cin >> values[i];

for (int i = 0; i < count; i++)
 total += values[i];
 
cout << "Average: " << total/count << endl;

values = nullptr;

Is the memory allocated for the values variable deallocated correctly?

 

True

False

False

35
New cards

Memory that is allocated with new, but never deleted with delete (or delete[]), will result in what?

 

A memory leak

An exception

Nothing at all

A stack overflow error

A memory leak

36
New cards

What do pointers point to?

 

variables

objects

memory addresses

variables

references

memory addresses

37
New cards

Where is dynamically allocated memory stored?

 

The stack

The heap

The heap

38
New cards

What is the preferred way of indicating that a pointer does not point to anything?

 

Setting it to 0

Setting it to delete

Setting it to nullptr

Setting it to NULL

Setting it to nullptr

39
New cards

What is the output of the following code?

void Foo(int& y)
{
    y = 10;
}

void Function(int& x)
{
    Foo(x);
    x += 2;
}

int main()
{
    int value = 5;
    Function(value);
    cout << value;

    return 0;
}

 

12

17

5210

5

12

40
New cards

What is the output of the following code?

void Function(int& x)
{
    x += 6;
}

int main()
{
    int value = 12;
    Function(value);
    cout << value;

    return 0;
}

 

18

126

12

6

18

41
New cards

What will the following code print out?

int x = 7;
int* one = &x;
x = 14;

int* two = &x;
*two -= 3;

cout << *one << *two;

 

714

711

77

1111

1111

42
New cards

Which of the following lines would create a constant variable?

 

constant float smallerPie = 3.141f;

const float twoPI = 6.28319f;

constfloat PI = 3.14159f;

float morePI const = 3.142f;

const float twoPI = 6.28319f;

43
New cards

Why do we pass or return variables by

const*

or

const&

 

To pass or return quickly AND prevent changes to them

To prevent changes to them

To pass or return them quickly

To pass or return quickly AND prevent changes to them

44
New cards

What is the Rule of Three?

 

If you define any members of the Big Three, the compiler will define correct versions for the other two.

If you define one member of the Big Three, you should define the other two.

If you don't define all members of the Big Three, the compiler will generate errors.

The programmer must always implement all members of the Big Three.

If you define one member of the Big Three, you should define the other two.

45
New cards

What class member function is called when an object falls out of scope?

 

Destructor

Default Constructor

Copy Constructor

Constructor

Destructor

46
New cards

If you don't define a copy assignment operator for your class...

 

the compiler implicitly defines one that will always work, no matter what

the compiler will generate an error

the compiler implicitly defines one that performs a member-to-member copy

nothing happens at all

the compiler implicitly defines one that performs a member-to-member copy

47
New cards

Which of the following is a copy assignment operator for a class Hero?

 

Hero& copy(const Hero& h);

Hero& operatorCopy(const Hero& h);

Hero& operator=(const Hero& h);

Hero& copyAssignmentOperator(const Hero& h);

Hero& operator=(const Hero& h);

48
New cards

Given a class, Matrix, what function is being called on the indicated line?

Matrix transform;
Matrix adjacent;
transform.SetIdentity();
transform.RotateX(3.14159f);
adjacent = transform;  <====== What function is called here?

 

copy assignment operator

default constructor

copy constructor

constructor

copy assignment operator

49
New cards

Given a class, Turret, what function is being called on the indicated line?

Turret mainTurret;
mainTurret.Initialize();

Turret secondTurret = mainTurret; <==== What function is called here?
secondTurret.SetPosition(100, 300);

 

default constructor

constructor

copy constructor

copy assignment operator

copy constructor

50
New cards

For a class Widget, identify the incorrect way(s) to call a copy constructor to copy an existing Widget named object1 to object2.

Mark all that apply.

Widget object2(object1);

Widget object2(&object1);

Widget object2.copy(object1);

Widget object2 = object1;

Widget object2(&object1);

Widget object2.copy(object1);

51
New cards

A copy constructor that does a member-to-member copy of all class variables, including dynamically allocated pointers, is performing what type of copy?

 

deep copy

pointer copy

reference copy

shallow copy

shallow copy

52
New cards

Which of the following are NOT members of the Big Three?

 Mark all that apply.

Copy constructor

Destructor

Default constructor

Constructor

Copy assignment operator

Default constructor

Constructor

53
New cards

Which of the following is a destructor for a class named Menu?

 

~Menu(int);

~Menu();

void ~Menu(delete);

void ~Menu();

~Menu();

54
New cards

Which of the following blocks of code would correctly create a copy of the dynamically allocated data that originalArray is pointing to?

Quaternion* originalArray = new Quaternion[10];
// Assume some cool stuff happens here

Block A

Quaternion* copy = originalArray;

Block B

Quaternion* copy = new Quaternion[10];
for (int i = 0; i < 10; i++)
    copy[i] = originalArray[i];

 

Block B

Block A

Block B

55
New cards

Given the following code, what is the purpose of const?

class Recipe {
   /* Assume complete definition */
};
class CookBook {
   Recipe *recipeList;
public:
   const Recipe* GetRecipeList();
};

const Recipe* CookBook::GetRecipeList() {
   return recipeList;
}

 

The prevent the returned pointer from being reassigned to another Recipe.

To protect the "this" pointer inside Cookbook.

To prevent changes to the recipeList variable through the returned pointer.

The protect the invoking object.

To prevent changes to the recipeList variable through the returned pointer.

56
New cards

What does the const keyword do in the following function?

float BankCustomer::GetTotalBalance() const
{
   float total = 0.0f;
   for(unsigned int i = 0; i < _accountNum; i ++)
   total += _accounts.GetBalance();
   return total;
}

 

Makes variables defined in the function const by default

Prevents overloading this function

Indicates that the function is "read only" by preventing changes to "this"

Makes the returned value const

Indicates that the function is "read only" by preventing changes to "this"

57
New cards

Given the following code:

int input;
cin >> input;
int* data = new int[input];
for (int i = 0; i < input; i++)
 data[i] = i * 5;

int* copy = data;
delete[] data;

What is the variable copy considered after deleting data?

 

A null pointer

A dangling pointer

A valid pointer

A deleted pointer

A dangling pointer

58
New cards

Template classes and functions allow a programmer to reuse code that operates on different:

 

numbers of function parameters

data types

types of member functions

numbers of member variables

data types

59
New cards

The Stack data structure stores information in a specific order. What order is that?

 

First in, first out

First in, largest out

Largest in, smallest out

Last in, first out

Last in, first out

60
New cards

Template classes should be split into header files and source files, like any other class.

 

True

False

False

61
New cards

Given a class called Widget, the following code is several examples of what?

bool Widget::operator==(const& Widget otherWidget) const;
Widget& Widget::operator=(const& Widget otherWidget);
Widget Widget::operator+(const& Widget otherWidget) const;

 

operator integration

operator initializing

operator overloading

operator overgrowing

operator overloading

62
New cards

What kind of limitation exists on the number of operators that can be implemented in any given class?

 

Hard limit, as defined by the language

Soft limit, defined by the individual programmer on a per-class basis

Soft limit, defined by the individual programmer on a per-class basis

63
New cards

The Queue data structure stores information in a specific order. What order is that?

 

Last in, first out

Oldest in, youngest out

First in, first out

Largest in, newest out

First in, first out

64
New cards

Which of the following are valid template declarations?

Mark all that apply.

 

typename <template NewType>

typename <template T>

template <typename T>

template <typename CustomType>

template <typename T>

template <typename CustomType>

65
New cards

Given the following code:

Game Game::operator+(Game newGame)
{
   Game myGame;
   myGame.score = score + newGame.score;
   
   //Return statement to be written here
}

What is the correct return statement for this operator?

 

return new Game;

return Game;

return this;

return myGame;

return myGame;

66
New cards

Given the following code, what will the output be?

Stack<int> stack;

for (int i = 0; i < 5; i++)
 stack.push(i);

int first = stack.pop();
cout << first;

4

67
New cards

Given the following code, what will the output be?

Queue<int> q;

for (int i = 2; i < 8; i += 2)
 q.enqueue(i);
 
cout << q.dequeue();
cout << q.dequeue();
cout << q.dequeue();

246

68
New cards

Which of the following is NOT a class that we can use to open files in C++?

 

ifstream

fstream

filestream

ofstream

filestream

69
New cards

When reading and writing files, where is the default location from which a program will open or create these?

For example, if a file was created like this:

ofstream exampleFile("test.txt");

Where would it get created?

 

The root directory of your hard drive

In your user documents directory

The location of your source code

In the same directory as the program itself

In the same directory as the program itself

70
New cards

Which of the following are absolute paths?

Mark all that apply.

 

data/playerSettings.zyx

D:/Documents/hpotter/magic/notes.spellbook

numbers.txt

C:/Bob/Unicorns/favorites.txt

D:/Documents/hpotter/magic/notes.spellbook

C:/Bob/Unicorns/favorites.txt

71
New cards

Where does the data in a data-driven program come from?

 

the source code of the program

files inside the program

the program itself

files outside the program

files outside the program

72
New cards

What delimiter does a CSV file use?

 

a colon

a semicolon

a comma

a carat

a comma

73
New cards

What operator can we use to read data from a file?

 

<=>

^

cin

<<

>>

>>

74
New cards

What function can we use to read an entire line from a file?

 

fromline

readline

inline

streamline

getline

inputline

getline

75
New cards

What operator do we use to write data to a file?

 

+=

<<

^

>>

cout

<<

76
New cards

A full path for a file is made of which of the following (one of which could technically have no value)?

Mark all that apply

 

a description of the file contents

an extension

a name

a directory

the name of the program that created it

a file size

an extension

a name

a directory

77
New cards

Which of the following characters could be used as a delimiter? Mark all that apply

 

a

,

|

%

?

$

a

,

|

%

?

$