C++ Language Flashcards

1
Q

What can you tell us about it: a conditional operator?

A

Its a notion for some kind of if else

Conditional expression: If else

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

Difference between conditional statement and a condition expression

A

With a condition expression you are choosing one of two expression based on a boolean outcome
Condition statement: you are selecting one of two statements based on a boolean outcome.
All languages have condition statements not all languages have condition expressions

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

Abstract classes in C++

A

A class that doesn’t contain member functions, but only contains prototypes

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

Difference between a java abstract class and interface

A
If it was an interface you cant have variables
In an abstract class you can have variable constants and prototypes 
In an abstract class you can have functions that are clearly defined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Major difference between java and c++?

A

The way they treat multiple inheritance

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

What is the difference between private and protected class members:

A
Private: are only accessible within the class defining them
Protected: are accessible in the class that defines them and in classes that inherit from that class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is “this.” Give an example of when you would use “this.”

A
"This" refers to the current object. Example:
public MyThisTest(int a){
this.a = a; // Assigns the value of the parameter a to the field of the same name
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the initialization and the assignment phases of a constructor? What is each used for?

A
The initialization phase, data members of class type are always initialized explicitly in the constructor initializer list. Initalization happens before any the statement execution of the constructor body.
Assignment phase is when one object is assigned the value of another object. This happens in the constructor body.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Having a pointer as a class data member can lead to some undesirable side effects. What should you add to a class to avoid these side effects?

A

You can add a delete destructor (which guarantees the reset of the pointer) or add an implementation of the free() function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
void you(long a, longb) {cout<< "long";}
void you(double a, doubleb){cout<;}
  int main a. b;
  you(a,b);
}
A

overloading function “you” while using same signature (is ambiguous). Code in 5a does not compile and thus does not run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
int main(){
  const char* what = "Is This";
  what = "interesting";
  cout<<< *what;
		}
A

Declaration of character array as a constant and cannot be changed does not compile and does not run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
int& Now(){
   int Where = 1;
   Where = Now();
   cout << Where;
   }
A

Sends a warning that reference to local variable where is returned but it complies and runs with an output of 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
class Where{
public:
int here;
int foo() const;
int bar() const;
};
	int Where :: foo() const{
		here = here + 5;
		return here;
		}
	int Where ::bar() const{
		return here + 5;
		}
	int OhNo(const Where* Now){
		return now->bar();
		}
	int main(){
		Where* IsIt = new Where;
		cout
A

/* foo() and bar() are set as constant numbers and are thus read-only and you cannot manipulate any of the data members inside of it(the function(s)). Also, int here was never initialized. Any references to here are null pointer exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
a. class Top{
public:
int a;
Top(int x) {a=x;}
};
	class Bottom : public Top{
	public:
		int b;
		Bottom() : Top(5) {b = 0; a = 0;}
		};
	int main(){
		Bottom barrel;
		cout
A

/* compiles and runs with output of 0 0 */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
class Top{
public:
void Hat(char *string){cout Hat(5.5)
		Rung->Hat(“cat”);}
A

//overloading function you while using same signature (is ambiguous). Code in 7b does not compile and thus does not run */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
class Top{
public:
Top(){cout
A

compiles and runs with output of: start top, start bottom, start test, end bottom, end top */

17
Q

Give the output of the following program:
#include
using namespace std;

	class Top{
	public:
		virtual void MyMemory(){cout MyMemory();
		Hat->Disk();
		Hat->ThisExam();
		Top Dog = *(new Bottom);
		Dog.MyMemory();
		Dog.Disk();
		Dog.ThisExam();
	}
A

/* this code does not compile and does not run because Erased() is not initialized in class Top. In inheritance parent’s method cannot be inherited from child.*/

18
Q
  1. List the different ways to implement implicit type conversion in a class:
    You can use a standard conversion. Example:
    short a =2000;
    int b;
    b=a;
A
You could use constructor or operator conversions, which affect 	classes that include specific constructors or operator functions 	to perform conversions.  Here, an implicit conversion happened 	between objects of class A and class B, because B has a 	constructor that takes an object of class A as parameter.  	Example:
		class A{};
		class B {public: B (A a) {} };
		A a;
		B b=a;
		//end example
19
Q
  1. a. True or false. A copy of a constructor does not have an initialization phase.
A

False

20
Q
How do the variables A and B differ?
char * const A = “Hi”; 
const char* B = “Hi”;
A
char * const A = “Hi”; //constant pointer to character data
const char* B = “Hi”; //character array that is constant
21
Q

Explain the problems with the following uses of C and D
const char* C = “hi mom”;
C[3] = ‘a’;

		char *const D = “hi mom”;
		D = “hi dad';
A
C[3] = 'a'; //can't change constant character array
D = “hi dad';  //constant pointer, can change the data but 					//...can't change where the pointer points
22
Q
#include 
using namespace std;

void functionA(int X, double Y) {cout

A

outputs: second function. Program has char set as x value so second function is implicitly called.

23
Q
#include
using namespace std;
class Test{
private:
	float data;
public:
	void setData(int&amp; value) {data = value;};
	static float getData() {return data;};
	Test(int value) : data(value) {};
};
int main(){
	Test me = 10.5;
	cout
A

/* giving it 10.5 when it’s asking for an integer */

24
Q
#include
using namespace std;

int A = 10;

float functionB(int A, char B = 5, float C){
	return ::A + B + C;
}
int main(){
	int A = 2;
	float X = 11.1;
	cout
A

/* cannot initialize a varianle in the argument section of a function */

25
Q

Amend the following code so that both C and C++ compilers will approve it

              int *p;
              p = malloc(20*sizeof(int));
A

//cast malloc to int( (int*)malloc(…);

26
Q

In C++ a class and a struct are extremely similar, and differ in only one way. How do they differ?

A

//structs are by default public and classes are by default private

27
Q

Explain how virtual base classes are used to solve one of the problems of multiple inheritance, and say which problem they solve.

A

Solve the problem of multiple inheritance ambiguity. There would be a problem if a grandchild class inherited from 2 parent classes and a grandparent method was called.

28
Q

What is the output of

cout &laquo_space;oct &laquo_space;44 &laquo_space;hex &laquo_space;55;

A

–> outputs 44 in octal and 55 in hexadecimal.

29
Q
  1. set_new_handler(…) serves what purpose as a C++ supplied method?
A

Make more storage available for a new attempt to allocate storage.

30
Q

Discuss the significant different between c++ and java as oop languages

A
  1. Only c++ has multiple inheritance
    \ Java does nto have the ability to get the address of the variable
  2. Java has no address arithmetic (ability to grab a address)
  3. Dynamic polymorphism is the only Java option
  4. Java has no external constants, variables, methods (more purely object oriented)
    Because you can’t have external functions
  5. c++ allows primitive operator overriding
    // In c++ you can over ride a + sign
  6. c++ is better at rpg (meaning it give you finer control over information displayed
    on a screen)
  7. How does java manage memory(Garbage collector)
    c++ does not do garbage collection therefore the programmer is requrired to manage his own memory
    Historically alot of people have made a good living by making it known by word of mouth that
    they were good a retofitting memory management
  8. Java handles memory by garbage collection
    • c++leaves it up to the programmer
      when you delete an array
      /////////////
      int (*a) [100000];
      delete [] a; // is how you delete an array
      // You need to clue the complier in that you want to delete the whole thing
  9. Java is more strongly typed
    because c++ inherits from c you aren’t really obligated to specify a return try on a function

classic different between java 1.1 and c++
java source code is translated into a byte code
c++ complier could produce byte code, trees, but they aren’t any obligation to produce the same intermediate form of code
9. Java has byte code and is interpretable: there are interpreters
Java is more portable
c++ has the potential to be faster
10. The fact that java is not standardized and c++ is
c++ get redefined promptly every 12 years
java is far more agile in terms of changes and api additions
the difference is standardization