Vector V (22) Bool B (V (6) ) Printf ("%D", !B) : Reference
Vector V (22) Bool B (V (6) ) Printf ("%D", !B) : Reference
vector<int> v(22);
bool b = (v[6]);
printf("%d", !b);
False
0
1
Q2. Which of the following is a reason why using this line is considered a bad
practice? (Alternative: Why is using this line considered a bad practice?)
If the code uses a function defined in two different libraries with the same
prototype but possibly with different implementations, there will be a
compilation error due to ambiguity.
It automatically includes all header files in the standard library (cstdint,
cstdlib, cstdio, iostream, etc).
It causes the compiler to enforce the exclusive inclusion of header files
belonging to the standard library, generating compilation error when a
different header file is included.
Reference
Q3. What is the smallest size a variable of the type child_t may occupy in
memory?
typedef struct{
unsigned int age : 4;
unsigned char gender : 1;
unsigned int size : 2;
}child_t;
7 bits.
25 bytes.
1 bit.
1 byte.
Reference
Q4. Which of the following shows the contents of vector v1 and v2 after
running this code?
std::vector<int> v1{1,2,3},v2;
v2=v1;
v1.push_back(4);
v2.push_back(5);
Error
v1:{1,2,3,4}; v2:{5};
v1:{1,2,3,4,5}; v2:{1,2,3,4,5};
v1:{1,2,3,4}; v2:{1,2,3,5};
Q5. Which of the following is a true statement about the difference between
pointers and iterators?
While pointers are variable that hold memory address, iterators are
generic functions used to traverse containers. These function allows the
programmer to implement read and write code as the container is traversed.
Pointers are variables that hold memory address where as iterator are
unsigned integers that refers to offsets in arrays.
All iterator are implemented with pointers so all iterators are pointers but
not all pointers are iterators.
Reference
There are no benefits because a reference and an object are treated as the
same thing.
Reference
union {
unit16_t a;
unit32_t b;
int8_t c;
} u1;
4 bytes
7 bytes
8 bytes
2 bytes
Reference
Q8. Which of the following operators is overloadable?
?:
new
::
.
Reference
Q9. Which of the following shows the contents of vector pointed by v1 and v2
after running this code?
*v1:{1,2,3,4}; *v2:{5};
*v1:{1,2,3,4,5}; *v2:{1,2,3,4,5};
Error
*v1:{1,2,3,4}; *v2:{1,2,3,5};
Q10. Which of the following is not a difference between a class and a struct?
Because structs are part of the C programming language, there are some
complexity between C and C++ structs. This is not the case with classes.
The default access specifier for members of struct is public, whereas for
member of class, it is private.
Template type parameters can be declared with classes, but not with the
struct keyword.
Reference
Q11. Suppose you need to keep a data struct with permission to access some
resource based on the days of the week, but you can't use a bool variable for
each day. You need to use one bit per day of the week. Which of the following
is a correct implementation of a structure with bit fields for this application?
A
typedef struct {
int sunday:1;
int monday:1;
// more days
int friday:1;
int saturday:1;
} weekdays;
B
C
typedef struct {
bit sunday:1;
bit monday:1;
// more days
bit friday:1;
bit saturday:1;
} weekdays;
D
typedef struct {
bit sunday;
bit monday;
// more days
bit friday;
bit saturday;
} weekdays;
NOTE: Correct syntax is that each variable size is 1 bit. bit is not a type in C+
+. Reference
Q12. What is an lvalue?
Q13. What does auto type specifier do in this line of code (since C++11)?
auto x = 4000.22;
It specifies that the type of x will be deduced from the initializer - in this
case, double.
It specifies that the type of x is automatic meaning that if can be assigned
different types of data throughout the program.
It specifies that more memory will be allocated for x in case it needs more
space, avoiding loss of data due to overflow.
skeleton source code for a class where the programmer has to fill in
specific parts to define the data types and algorithms used.
Reference
if(x)
y=a;
else
y=b;
y=a?b:x;
y=if(x?a:b);
y=(x&a)?a:(x&b)?b:0;
y=x?a:b;
Reference
#include <iostream>
int main(){
int x=10, y=20;
std::cout << "x = " << x++ << " and y = " << --y << std::endl;
std::cout << "x = " << x-- << " and y = " << ++y << std::endl;
return(0);
}
x = 10 and y = 20
x = 11 and y = 19
x = 11 and y = 19
x = 10 and y = 20
x = 10 and y = 19
x = 11 and y = 20
x = 11 and y = 20
x = 10 and y = 19
Q17. What is the meaning of the two parts specified between parentheses in a
range-based for loop, separated by a colon?
The first is a variable declaration that will hold an element in a sequence.
The second is the sequence to traverse.
The first is an iterator, and the second is the increment value to be added
to the iterator.
int8_t a=200;
uint8_t b=100;
if(a>b)
std::cout<<"greater";
else
std::cout<<"less";
greater
less
Part A executes because x==5 (true) and y==2 (true), thus the AND
operation evaluates as true.
A
B
C
D
Q21. Which STL class is the best fit for implementing a collection of data that is
always ordered so that the pop operation always gets the greatest of the
elements? Suppose you are interested only in push and pop operations.
std::list
std::vector
std::priority_queue
std::map
Q22. What is the meaning of the three sections specified between parentheses
in a for loop separated by semicolons?
The first is the iterating variable name, the second is the number of times
to iterate, and the third is the desired increment or decrement (specified with
a signed integer).
The first is the initialization block, the second is the condition to iterate,
and the third is the increment block.
The first is the iterating variable, the second is the container in which it
should operate, and the third is an exit condition to abort at any time.
The first is the iterating variable name, the second is the starting value for
the iterating variable, and the third is the stop value (the last value plus one).
int i = 0;
printf("%d", i++);
printf("%d", i--);
printf("%d", ++i);
printf("%d", --i);
0,1,1,0
0,1,0,1
0,0,1,0
1,0,1,0
Reference
c is d and d is c
c is A and d is 3
c is 3 and d is A
c is c and d is d
printf("1/2 = %f",(float)(1/2));
1/2 = 0.499999
1/2 = 0
1/2 = 0.000000
1/2 = 0.5
Q27. What is the difference between a public and a private class member?
Public members are the same as global variables, so every part of the
code has access to them. Private members are the same as automatic
variables, so only their class has access to them.
3
7
-3
13
Q30. Consider a pointer to void, named ptr, which has been set to point to a
floating point variable g. Which choice is a valid way to dereference ptr to
assign its pointed value to a float variable f later in the program?
float g;
void *ptr=&g;
float f=*(float)ptr;
float f=(float)*ptr;
It is the same as the class member access operator, or arrow operator (-
>), which allows you to access a member of an object through a pointer to the
object.
It is the member access with address of operator, which returns the
address of a class or struct member.
Reference
Q32. For these declarations, which choice shows four equivalent ways to assign
the character "y" in the string to a char variable c?
A
c = buff[16];
c = str[5];
c = *(buff+16);
c = *(str+5);
B
c = *(buff[15]);
c = *(str[4]);
c = buff+15;
c = str+4;
C
c = buff[15];
c = str[4];
c = *(buff+15);
c = *(str+4);
D
c = *(buff[16]);
c = *(str[5]);
c = buff+16;
c = str+5;
Q33. Which choice is the correct declaration for the class named Dog, derived
from the Animal class?
class Animal{
//....
}
A
B
C
D
#include <cstdio>
using namespace std;
int main(){
char c = 255;
if(c>10)
printf("c = %i, which is greater than 10", c);
else
printf("c = %i, which is less than 10", c);
return 0;
}
Q36. Which choice is not a valid type definition of a structure that contains x
and y coordinates as integers, and that can be used exactly as shown for the
variable named center?
coord center;
center.x = 5;
center.y = 3;
A
B
C
typedef struct {
int x;
int y;
} coord;
D
struct coord {
int x;
int y;
};
Q37. Which choice does not produce the same output as this code snippet?
Assume the variable i will not be used anywhere else in the code.
for (i=1;i<10;i++){
cout<<i<<endl;
}
A
i=1;
while(i<10){
cout<<++i<<endl;
}
B
C
i = 1;
do {
cout<<i++<<endl;
} while(i<10);
D
i = 1;
loop:
cout<<i++<<endl;
if(i<10) goto loop;
#include "library.h"
It causes the toolchain to compile all the contents of library.h so that its
executable code is available when needed by the final application.
It cherry picks library.h for the declarations and definitions of all data and
functions used in the remainder of the source file main.cpp, finally replacing
the #include directive by those declarations and definitions.
It informs the linker that some functions or data used in the source file
main.cpp are contained in library.h, so that they can be called in run time. This
is also known as dynamic linking.
Q39. Consider this function declaration of is_even, which takes in an integer and
returns true if the argument is an even number and false otherwise. Which
declarations are correct for overloaded versions of that function to support
floating point numbers and string representations of numbers?
bool is_even(int);
A
B
C
D
A
#ifdef MY_LIBRARY_H
#define MY_LIBRARY_H
// my_library.h content
#endif /* MY_LIBRARY_H */
B
#ifndef MY_LIBRARY_H
#define MY_LIBRARY_H
// my_library.h content
#endif /* MY_LIBRARY_H */
C
#ifdef MY_LIBRARY_H
#undef MY_LIBRARY_H
// my_library.h content
#endif /* MY_LIBRARY_H */
D
#define MY_LIBRARY_H
#include MY_LIBRARY_H
// my_library.h content
#undef MY_LIBRARY_H
Q41. What's wrong with this definition when using a pre-C++11 compiler?
std::vector<std::vector<int>> thematrix;
>> is parsed as the shift-right operator, and thus results in a compile error.
sprite->x
sprite.x
sprite.*x
(*sprite).x
*sprite.x
A
B
D
int32_t nums[3]={2,4,3};
std::cout << ( nums[0] << nums[1] << nums[2] );
256
0
243
1 3 4 6 11
0 2 3 5 10
1 3 3 5 10
Q47. Which of the following STL classes is the best fit for implementing a
phonebook? Suppose each entry contains a name and a phone number, with no
duplicates, and you want to have lookup by name.
std::priority_queue
std::list
std::vector
std::map
Reference
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream file1("text1.txt", ios::binary);
ofstream file2("text2.txt", ios::binary);
file2 << file1.rdbuf();
}
It copies the contents of text1.txt into text2.txt - i.e., it makes a copy of
text1.txt, named text2.txt.
It appends the contents of text1.txt into text2.txt - i.e., replaces the
contents of text2.txt by the concatenation of text2.txt and text1.txt.
Reference
class my_class {
public: static int count;
}
The variable cannot be modified by any part of the code in the same
application or thread. However, other threads may modify it.
The variable exists even when no objects of the class have been defined so
it can be modified at any point in the source code.
The variable is allocated only once, regardless of how many objects are
instantiated because it is bound to the class itself, not its instances.
All objects that try to access their count member variable actually refer to
the only class-bound static count variable.
Reference
Q50. What is the assumed type of a constant represented in the source code
as 0.44?
double
long float
long double
float
Q51. What is the output of this piece of code?
int8_t a=200;
uint8_t b=100;
std::cout<<"a="<<(int)a;
std::cout<<", b="<<(int)b;
a=-56, b=100
a=-55, b=100
a=200, b=-156
a=200, b=100
delete(my_object);
free(my_object);
Q53. What is the correct way to call the count member function for the object
pointer called grades?
class my_array{
public:
int count();
}; // ... more members above
int main(){
my_array *grades = new my_array();
}; // ... more code above
grades.count();
my_array->count();
grades->count();
my_array.count();
Reference
846
The output is the addresses of i2, i0, and i1, in that order, with no spaces.
468
Reference
Q55. Does this code cause a compiler error? If so, why, and if not, what
is child_t?
typedef struct{
unsigned int age : 4;
unsigned char gender : 1;
char : 0;
unsigned int size : 2;
}child_t;
Yes, it causes a compiler error because the colon character is not allowed
in struct definitions.
and child_t is a type defined as a structure with bit fields. It has 4 bits for
age and 1 bit for gender in the first byte, and 2 bits for size in the second byte.
Yes, it causes a compiler error because one field is defined as having a size
of 0.
Reference
A->B->C->D
A.B.C.D
*A.*B.*C.*D
&A.&B.&C.&D
*(*((*A).B).C).D
It declares a memory buffer named buff that starts at address 20 and ends
at address 70.
It sets all bits in the array named buffer from its element at index 20 to its
element at index 50.
It writes the value 20 in every memory address from buff to buff+49.
It declares a memory buffer named buff that starts at address 20 and ends
at address 50.
Reference
CustomData& operator++();
void operator++(CustomData);
CustomData operator++(CustomData);
CustomData operator++(int);
Reference
Q59. You want to sort my_array, declared below. Which choice is the correct call
to std::sort, using a lambda expression as the comparison function?
std::array<uint32_t, 50> my_array;
A
std::sort(my_array.begin(), my_array.end(),
[](uint32_t a, uint32_t b) {
return a < b;
})
B
C
std::sort(my_array.begin(), my_array.end(),
lambda(uint32_t a, uint32_t b){
return a < b;
})
D
Reference
A
void std::mutex::lock(){
while(!this->try_lock());
}
B
void std::mutex::lock(){
return (this->try_lock());
}
C
void std::mutex::lock(){
while(1)
this->try_lock();
}
D
void std::mutex::lock(){
while(this->try_lock());
}
It allows the programmer to write the necessary code to free the resources
acquired by the object prior to deleting the object itself.
Q62. Which STL class is the best fit for implementing a phonebook? Suppose
each entry contains a name and a phone number, with no duplicates, and you
want to have lookup by name.
std::priority_queue
std::map
std::vector
std::list
std::mutex::lock()
std::mutex::try_lock()
lock() has a higher privilege over try_lock(). This means that you have a
better chance of acquiring a mutex with lock().
Reference
There are no benefits because a reference and an object are treated as the
same thing.
a compiler option that prevents the user code from including additional
libraries
a preprocessor statement that prevents a source file from being included
more than once in a project
a library that adds safety features such as mutexes, watchdog timers, and
assertions to the project
Q66. What would be the correct declaration of a default constructor for a class
named Sprite?
public:
Sprite();
private:
void Sprite();
public:
void Sprite();
private:
Sprite();
#pragma once
to restrict the use of its contents to only one source file
to tell the compiler that only one variable can be instantiated from the
classes or types contained in this header file
to help the compiler finish faster by assuring that only one compiler pass
is neccessary for the code included in this header file
to make the compiler parse that header file only once, even if it is
included multiple times in the source
reference here
a 2-tuple
Reference
bool is_even(int);
Q70. Other than shifting bits to the left, what is the << oprator used for ?
Yes, it causes a compiler error because the colon character is not allowed
in struct definitions.
and child_t is a type defined as a structure with bit fields. It has 4 bits for
age and 1 bit for gender in the first byte, and 2 bits for size in the second byte.
Yes, it causes a compiler error because one field is defined as having a size
of 0.
Reference
The compiler needs the dara type to make sure that the pointer is not
going to be used on illegal non-pointable types such as functions, labels,
pointers, and reference.
void * does not work for any type. The language does not allow assigning
anything other than void to a pointer to void *.
The compiler needs the data type to know how much memory to allocate
for the pointer, because different data types require different pointer lengths.
Yes, it causes a compiler error because one field is defined as having a size
of 0.
Reference