Midterm Flashcards

1
Q

_______ declared _____ are meaningful only within

the file they are declared in

A

Globals declared static are meaningful only within

the file they are declared in

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

Globals visible in other files by declaring it as _____

A

extern

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

Global variables are in scope from moment of declaration until ___ ___ ___

A

end of file (not in-scope earlier in file)

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

a variable defined inside a _____ is
meaningful only inside that function
• It is said to have _____ scope

A

a variable defined inside a function is
meaningful only inside that function
• It is said to have local scope

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

int count = 0; // Create a global variable

int count_items()
{
	int orig_count = count;
int count = 3;

for (int i = 0; i < 4; ++i)
	count++; 
return count + orig_count; }
what is the output?
int main() 
{ cout << count << endl;
cout << count_items();
}
A

0

7

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

When an inner variable has the same name as an outer variable, the outer variable is _______ by the inner variable

A

When an inner variable has the same name as an outer variable, the outer variable is shadowed by the inner variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
double balance; // Balance has global scope
class BankAccount
{
public:
BankAccount(double balance);
// Parameter variable balance has local scope
private:
double balance;
// Data field balance has class scope
};
BankAccount::BankAccount(double balance)
{
this->balance = balance;
// Assign \_\_\_\_ \_\_\_\_\_\_ \_\_\_\_\_\_ using parameter
value
\::balance = balance;
// Assign \_\_\_\_\_\_ \_\_\_\_\_\_ using \_\_\_\_\_\_ value
}
A
double balance; // Balance has global scope
class BankAccount
{
public:
BankAccount(double balance);
// Parameter variable balance has local scope
private:
double balance;
// Data field balance has class scope
};
BankAccount::BankAccount(double balance)
{
this->balance = balance;
// Assign data field balance using parameter
value
\::balance = balance;
// Assign global variable using parameter value
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is definition of variable scope?

A

Where a name is visible

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

What is definition of variable lifetime?

A

How long a variable exists during program execution

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

• A static local variable is allocated and initialized when
the program loads, and exists until _____ _____
• Only visible to __ _______

A

• A static local variable is allocated and initialized when
the program loads, and exists until program exits.
Only visible to the function

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

What functions can access protected sections of a class?

A

functions within that class and children of that class

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

How should the protected designation be used?

A

Same caution as public. Only accessors

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

• It is common to grant friendship to the _____ operators

A

• It is common to grant friendship to the stream operators

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

Friend functions and classes can access what data of the class friendship is defined in?

A

Can access all protected and private data

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

Substitution principle: If B inherits from A, then ______

_____________________

A

Substitution principle: If B inherits from A, then B can be used wherever A is expected

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

How does inheriting privately from a base class work?

A
The child class can access base class methods only 
internally and member fields do not flow through. The child class objects can not access base class methods.
17
Q

What is the syntax for declaring a namespace?

A
namespace name_space_name
{
class Example 
   {
   };
}
18
Q

What is the syntax for dumping a namespace into global namespace?

A

using namespace name_space_name;

19
Q

Namespaces should be short or long and descriptive?

A

long and descriptive

20
Q

How are namespaces shortened to a manageable length?

A

Aliasing

nsn = name_space_name;

21
Q

How do you dump an individual name into the namespace. (ex: std::cout)

A

using std::cout;

22
Q

namespaces should NEVER be used in what type of file?

A

header (included) files because the user does not necessarily know what namespace they are dumping into their global namespace and name ambiguities can occur.

23
Q

A destructor is automatically invoked when ______ ______ _________

A
A destructor is automatically invoked when a class
variable is no longer in scope.
24
Q

Two syntexes for calling copy constructor

A
String first("Fred");
String second(first);

String second = first;

25
Q

If the user does not write a copy constructor, do copy constructors exist?

A

Yes the compiler automatically creates one however it uses memberwise copying

26
Q

What is the problem with the constructor provided by the compiler?

A

Creates only shallow memberwise copying. Problem when copying pointers

27
Q

What is the syntax of a copy constructor?

A

String::String(const String& right) {}

28
Q

What are the three things that an assignment operator must do?

A

Check for self assignment
Clean up existing resources
Create a copy of values in rhs

29
Q

What are the 8 instances when a destructor is called?

A
  • Local variables, at end of block
  • Parameters, at end of function
  • Temporary variables, at end of statement
  • Heap values, when delete is called
  • Static values, when main terminates
  • Member data, when object is deallocated
  • Base class object, when derived class object is deallocated
  • Local variables, when an exception is thrown and execution
    leaves the block
30
Q

What are the six memory management functions?

A

1.Copy constructor
2.Assignment operator
3. Destructor
4.Default Constructor
5.Move Constructor (faster than a copy constructor because it
doesn’t allocate memory nor does it copy memory buffers).
6.Move Assignment operator

31
Q

What is the assignment operator syntax?

A

&Example Example::operator=(const Example& rhs) {}

32
Q

What would happen if you supplied a destructor but no constructor? And one shallow copy of an object went out of scope before the original or vise versa?

A

The destructor would be called to reallocate resources and an error would emerge if the object was used because it referenced resources that are no longer allocated

33
Q

Each parameter must be preceded by the keyword _____ or ______

A

Each parameter must be preceded by the keyword class or typename

34
Q

double e = max(2, 3.13)

A

Why isn’t this allowed? How can it be fixed?

double e = max(2, 3.13);

35
Q
What is the template statement above this class definition?
class Pair
 { public:    
Pair(const F&amp; a, const S&amp; b);   
 F get_first() const;   
 S get_second() const; 
private:    
F first;    
S second; };
A

template

36
Q

What are the four conditions for when paint event is called?

A
  1. The widget is shown for the first time.
  2. The widget is resized.
  3. The widget is obscured by another window, and then is revealed again.
  4. We force it by a call to repaint() or update().
37
Q

Write an int main for a qapp

A
#include 
#include 
int main(argc, *argv[])
{
QApplication(argc, argv);
MainWindow w;
w.show();  

return app.exec();
}