int num = 5;
int* ptr = #
std::cout << ptr;
What does this output?
- 5
- an address
an address
Explanation:
ptr stores the memory address of num
ptr* stores the value of num
What is the extension to open a file in binary mode?
ios::binary
class Person {
string name;
int age;
};
Is this class public or private?
private
Because classes are private by default
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
};
int main() {
Dog d;
std::cout << d.legs;
}
What is printed?
A) 0
B) 4
C) error
D) garbage
B) 4
void setTitle(const std::string& title);
What does const here prevent?
A) function from being called
B) parameter from being modified inside function
C) return value from changing
D) object creation
B) parameter from being modified inside function
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array;
std::cout << *(array + 3);
What number does this display?
a. 2
b. 4
c. 3
b. 4
Explanation: indexing starts at 0, and that value is 4
does tellp() return the reading position or the writing position?
returns the current write position
class Car {
public:
int speed;
};
int main() {
Car c;
c.speed = 50;
std::cout << c.speed;
What is printed?
A) 0
B) 50
C) garbage value
50
class Base {
public:
void show() {
std::cout << "Base";
}
};
class Derived : public Base {
public:
void show() {
std::cout << "Derived";
}
};
int main() {
Derived d;
d.show();
}
What is printed?
A) Base
B) Derived
C) BaseDerived
D) error
B) Derived
const std::string& getTitle() const;
What does the last const mean?
A) function cannot be called
B) function is static
C) return value is constant only
D)function cannot modify the object
D)function cannot modify the object
int arr[] = {1,2,3,4,5,6};
std::cout << &arr[4] - arr << std::endl;
What would this output?
a. 4
b. 3
c. 5
a. 4
Because &arr[4] - arr calculates how many positions apart the two pointers are, which is 4 elements from the start
what sets an error flag for a bad hardware issue?
a. file.eof();
b. file.fail();
c. file.bad()
c. file.bad()
What is printed?
A) 5
B) 8
C) 3
D) garbage
B) 8
class Animal {
public:
virtual void sound() {
std::cout << "Animal";
}
};
class Cat : public Animal {
public:
void sound() override {
std::cout << "Meow";
}
};
int main() {
Animal* a;
Cat c;
a = &c;
a->sound();
}
What is printed?
A)Meow
B)Animal
C) Cat
D) error
A)Meow
std::unique_ptr<int> p2 = std::move(p1);
What happens to p1?
A) becomes nullptr
C) deleted immediately
B) still valid
D) copied
A) becomes nullptr
int** ptrArr = new int*[5];
for(int i = 0; i < 5; i++){
ptrArr[i] = new int(i * 10);
}
std::cout << *ptrArr[3] << std::endl;
What is printed?
a. 30
b. 40
c. 20
a. 30
What does this do to a file?
file.seekg(0L, ios::end);
moves the file reading cursor to the end of the file
class Item {
public:
int price = 20;
};
int main() {
Item i;
Item* ptr = &i;
ptr->price = ptr->price + 10;
std::cout << i.price;
}
What is printed?
A) 20
B) 10
C) 30
D) address
C) 30
class Base {
public:
int x = 5;
};
class Derived : public Base {
public:
int y = 10;
};
int main() {
Derived d;
Base b = d;
std::cout << b.x;
}
What is printed?
A) 5
B) 10
C) error
D) garbage
A) 5
How long has Mr. Atkinson been teaching at TCC
a. 19 years and 11 months
b. 17 years and 3 months
c. 19 years and 9 months
d. 18 years and 6 months
c. 19 years and 9 months
int arr[4] = {10, 20, 30, 40}; int* p1 = arr; int* p2 = arr + 2; int** ptr = &p1;
*ptr = p2;
std::cout << **ptr << std::endl;
What is printed?
A) 10
B) 20
C) 30
D) 40
C) 30
Because *ptr = p2 makes ptr point to arr + 2, and dereferencing twice (**ptr) gives the value at index 2, which is 30.
char obj;
ifstream file("data.bin", ios::binary);
file.read(reinterpret_cast<char*>(&obj), sizeof(obj));
It reads 1 byte from the file and stores it directly into obj
class Box {
public:
int size = 3;
};
int main() {
Box b;
Box* p = &b;
p->size++;
p->size = p->size + 4;
std::cout << b.size;
}
What is printed?
A) 3
B) 4
C) 8
D) 7
C) 8
class Base {
public:
void speak() {
std::cout << "Base";
}
};
class Derived : public Base {
public:
void speak() {
std::cout << "Derived";
}
};
int main() {
Base* ptr;
Derived d;
ptr = &d;
ptr->speak();
}
What is printed?
A)error
B) Derived
C)Base
D) BaseDerived
C)Base
What is the main purpose of a pch.h file in a C++ project?
A) It makes your program run faster at runtime
B) It stores all your functions in one place
C) It speeds up compilation by preloading common headers
D) It replaces the need for include statements completely
C) It speeds up compilation by preloading common headers