C++ Midterm Flashcards

1
Q

State the following C/C++ expression below abbreviates:

a[8] ______________

A
  • (a + 8)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Expand the Acronym RTTI

A

Run time Type Identification

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

Briefly describe ‘reflection’ in java

A

The ability to learn at runtime the class to which an object belongs, and properties of that class. Basic tools: getClass(), instanceOf

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

T/F java allows multiple inheritance

A

F

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

T/F java does not allow pointer arithmetic

A

T

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

C allows branching into the middle of a block but c++ does not

A

F

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

in c++ an int must contain precisely 32 bits in binary

A

F

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

C++ allows the statement 17;

A

T

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

in C++ a struct can contain both fields and member functions

A

T

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

Java and C++ have precisely three access modes

A

F

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

the object class is inherited by each java class

A

T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Assume a class declared as 
class stuff {
  string a,b;}
provide a single constructor for stuff which works as follows: the constructor can be called with two, one or zero parameters.
A
public stuff(string c = "", string d = ""){
      a = c;
      b = d;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Assume a class declared as 
class stuff {
  string a,b;}
provide a copy constructor for this class which does not result in 'sharing' of the string value for the two copies
A
stuff(const staff &s) {
    stuff temp = new staff();
          temp a = s *a;
          temp b = s *b;
   return temp;}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Assume a class ‘rational’ for rational number. Give two prototypes for an override of +, one a friend function, and the other not. Indicate how each is called by giving example code snippets.

A

friend rational operator + (rational r1, rational r2)
rational operator + (rational r)
first called by roat = r1 + r2; [r + (r1,r2)]
2nd called by roat = r1+(r2) [or r1+r2]

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

Suppose we have, assuming a rational number class,
const rational half = new rational (1,2);
Given the half is constant, what kind of messages can be sent to half? Indicate the syntax of a valid method being use this way.

A

Message must be “constant member function”

foo(___) const{____}

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

Briefly discuss whether Java or C++ is better about allowing a user to write his own exception handlers for runtime exceptions

A

Java is better: user can control dozens of checked exception handlers using catch

17
Q

Describe a specific runtime exception in c++ where the user can redo the default exception handler

A

heap underflow

18
Q

In C using the static modifier on a global constant affects scope: Explain

A

Changes scope from program scope to file scope

19
Q

In C++ the scope resolution operator can be used as a unary operator (one input) although it usually has two. What is the unary version good for?

A

;;a is used to refer to a global entity named ‘a’

20
Q

How was the io class fstream used in an example given in class? What ‘kind’ of io was it used for?

A

The two uses are random io and update io class example was random io

21
Q

What is the main difference between a C++ parametrized macro and a C++ inline method that is declared in a class but defined external to a class? Give very short examples of each

A
Big difference is absense of type checking with a macro
Ex:
#define foo(a,b)    (a+b)/2

inline int foo(int i, int i){
return (i +i)/2}

22
Q

State how to determine, by looking at the code, if a class is C++ is an abstract class.

A

It should contain at least one pure fn,

ex: void foo() = 0;

23
Q

Describe briefly the difference between an abstract Java class and a Java interface.

A

Abstract class can contain variables and defined methods, Interfaces only constants and method prototypes. Also can only inherit one abstract class, but can implement many interfaces

24
Q

If subclass sub privately inherits public variable x from class super, state the accessibility of x within sub

A

private

25
Q

If subclass sub privately inherits public variable x from class super, state the accessibility of x within subclass subsub of sub, assuming subsub publicly inherits from sub

A

completely inaccessible

26
Q

The most important reason in C++ for using new instead of malloc is

A

ability to display any class, by over riding “from”

27
Q

Briefly state how ambiguity result in ‘diamond’ situations, when multiple inheritance is allowed, What ‘kind’ of base class helps to avert this problem in c++

A
A var in class A would have onr or two copies in D, and virtual value can be unclear
C++: virtual base class
28
Q

State the default field width when using cout, and explain what it means

A

default width is zero, and space given is minimal amount to display the item

29
Q

Give two ways to change the fill char., precisely one of which is a manipulator

A

&laquo_space;setfill(‘ ‘)

count.fill(‘ ‘);

30
Q

Discuss the significance of the C++ keyword ‘boolalpha’

A

when in effect, displays booleans as true or false

31
Q

Which retains its effect the longer, oct, or setw?

A

Oct wins

32
Q

Give the output for

cout««44;

A

2CoX2C

33
Q

The 4 io status bits have the names, good,

A

bad, fail, eof.

34
Q

Suzie Q, a produc of the legendary California K-12 system, says that Java has many more major releases and upgrades than C++ because the people that work for Java are all addicted to caffeine, and Duke is a slave driver. Is Suzie Q right?

A

The claim is correct, java has more releases! But reason is wrong: java is not standardized but C++ is, making release much slower

35
Q

In java, a public method in a parent class can not be override in a derived class and make private. Why does java say it has this rule? Does C++ share this rule?

A

Making a method public is a contract fn outside world accessibility, that extends to descendants. C++ does not share this rule, it can demote access in children

36
Q

Briefly explain the difference between overloading and overriding

A

Overloading: multiple fns in same class, with same name
Overriding: fns with same name in parent and child classes

37
Q

Sate briefly what constitutes a valid overload in c++

A

fn defs must have distinct signatures to be a valid overload