Exam 1 Flashcards

(61 cards)

1
Q

True or False

A struct data type can include variables.

A

True

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

True or False

A struct data type include functions.

A

True

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

Variables declared in a struct data type are called

A

Data Fields / Data Members

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

Functions declared in a struct data type are called

A

Member Functions

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

True or False

A function prototype require names of parameter variables

A

False

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

Why don’t function prototypes require names of parameter variables?

A

Variable names are not required in the function prototype. The compiler will ignore them. This also means that the variable names used in the function prototype do not need to match those used in the actual function itself.

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

When you define a new data type, Role or Employee, in our projects, in a separate header file, what preprocessing directives need to be added?

A

endif

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

Explain the reason why you include preprocessing directives

A

endif specifies the end of a conditional directive

This prevent from the multiple inclusion of same header file multiple time.

The #ifndef , if not defined, directive checks if the identifier has not been defined, or if its definition has been removed with #undef, the condition is true (nonzero). Otherwise, the condition is false (0).

The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.

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

When a singly linked list is used to store a collection of data items, each data item is stored in a _________, which includes an additional variable whose data type is a _______________.

A

Node , Node pointer

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

If you keep a variable that points to ________ , you can visit each data item in a single linked list.

A

Head

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

The arrow operator (->) is used when _______________

A

The arrow operator is used with a pointer to an object.

To access members of a structure through a pointer

a->b is the same notation as (*a).b

. Takes precedence so you have to () to do *a first.
-> is short hand

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

In a singly linked list, the pointer in the last node in the list typically points to ________

A

NULL

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

If you have a node pointer named head, which is pointing to a valide node, how do you access the member variable in the node named item?

A. head.item
B. *head.item
C. (*head).item
D. head->item
E. All of the above
F. A or B
G. C or D
A

G. C or D

(*head).item

head->item

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

Given the following declarations, how would you know if head is pointing to an empty list?

struct Node
{
    int number;
    Node* next;
};
Node* head;
// some other code here
A. if(head->next == NULL)
B. if(head == null)
C. if(head == NULL)
D. if(head->next==null);
E. A and D
F. B and C
A

C. if(head == NULL)

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

The actual value of NULL is

A. -1
B. 0
C. 99999
D. -99

A

B. 0

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

When do you use typedef? Write a code of an example

A

Define an alias of existing data type

typedef Employee EMP;

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

Which of the following loops correctly uses inter in order to move through the nodes of the linked list?

Node *iter;

A. while(iter != NULL)
iter++;
B. while(iter != NULL)
iter = iter->link;
C. for(iter =NULL; iter != NULL; iter = iter->link)
D. for(iter = head; iter != NULL; iter = iter->link)

A

D. for(iter = head; iter != NULL; iter = iter->link)

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

__________ is to create an instance and initialize data meme era of a struct data type

A

Constructor

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

The name of a constructor of a struct data type named Employee is ____________

A

Employee

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

The return of a construct of a struct data type named Employee is __________

A

none

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

The constructor of a struct data type named Employee without parameter variables is called _______________

A

Default constructor

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

Can a struct data type have multiple constructors ?

A

Yes

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

Explain why a struct data type have multiple constructors

A

Constructors are member functions. A function can be overloaded

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

How do you identify which constructor of a struct data type is called when an instance of the type is created?

A
Node* p1 = new node;
Node* p2 = new node(e);

Node(EMP);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Write a code that creates an instance of Employee at run time (, or dynamically), assign a salary to the instance, and display it on the console
Employee* p = new Employee; p->salary = 1000; cout << p->salary << endl; delete p;
26
What is the advantage and disadvantage of using an array compared to using a singly linked list when storing a collection of data items?
Array size is fixed when allocated Disadvantages - if array is full and more data items are needed, you have to resize the array, allocate new array, copy items from existing array one at a time, and delete existing array. Advantages - using pointer arithmetic to access next or previous item.
27
When you define a default constructor of Employee type outside the declaration of Employee type, you write __________ as the header of the function.
Employee::Employee();
28
You have main.cpp to compile on the CS server. What is a command to compile the program?
g++ main.cpp
29
What is the command to list all the files and directories of the current directory you are in?
ls -al
30
WinSCP is a program that ______________
Transfers files from local comp to server
31
Putty is a program that ____________
Emulates a terminal of a remote server
32
What is the keyword to use to define a class named DB?
class
33
Can a class data type include variables?
Yes
34
Can a class data type include functions?
Yes
35
Variables declared in a class data type are called _____
Data members
36
Functions declared in a class data type are called _______
Member function
37
A member function of a class whose name is the same as that of the class called ________
Constructor
38
A variable whose type is a class is called _______
Class variable
39
The name of a constructor of a class data type named DB is _______
DB()
40
The return of a constructor of a class data type named DB is ______
None
41
The constructor of a class data type named DB without parameter variable is called.
Default constructor
42
When a class do not define its constructor, the compiler creates __________. In this case, the values of the data members of the object.
Default constructor
43
When an object of a class is created, what will be the values of the data members of the object?
Depends on which constructor is called to create that object. If the constructor called did not initialize member variables, the data members have garbage values
44
________ allows multiple functions or member functions of a class to use the same function name as long as their _______ are different.
Function overloading, signatures
45
The signature of a function in C++ is uniquely determine by ______ , ______ , and ______. Return type may be/may not be part of the signature of a function, depending on what compiler is used.
Function name, # of parameter variables, data type of each parameter variable in order
46
A variable that is declared within a function, whether it is regular or a member of a class/struct is _______ to the _______ and the memory that is occupied by the variable is deleted when ______.
Local, function, the function returns
47
In each do the following code, write the prototype of a constructor that is called and complete each line. new DB; new DB(10);
new DB; DB(); new DB; DB(int);
48
What is the difference between a struct and a class in C++?
Class members Default to private but can be declared private or public. Struct members default to public.
49
The keyword, private and public, in the class declaration are called ________
Access specifiers
50
What is the difference between private members and public members
The functions that are not a member of the class can not directly access private members. A public member is accessible from anywhere outside the class
51
In the following code segment, what will be stored in the stack and in the heap of the program respectively? ``` int main() { double x; int *p = new int; int* q = new int[3]; } ```
x, *p, *q - stack int, int[3] - heap
52
What will be the printed values of x and y? void increaseByOne(int* &, double*); ``` int main()  {      int x;     double y;     int* p = &x;      double* q = &y;     *p = 3;     *q = 3.14;          increaseByOne(p, q);
 cout << x << " " << y << endl; cout << p << " " << q << endl; return 0; } ``` ``` void increaseByOne(int* &p1, double* q1) { (*p1)++; (*q1)++; p1++; q1++; } ```
``` x = 4 y = 4.14 ```
53
Will the value of p be increased after increasedByOne() returns? Why / why not? void increaseByOne(int* &, double*); ``` int main()  {      int x;     double y;     int* p = &x;      double* q = &y;     *p = 3;     *q = 3.14;          increaseByOne(p, q);
 cout << x << " " << y << endl; cout << p << " " << q << endl; return 0; } ``` ``` void increaseByOne(int* &p1, double* q1) { (*p1)++; (*q1)++; p1++; q1++; } ```
The value of p will increase because the parameter data type in the function is a reference of a pointer that increments arithmetically.
54
Will the value of q be increased after increasebyOne() returns? Why / why not ? void increaseByOne(int* &, double*); ``` int main()  {      int x;     double y;     int* p = &x;      double* q = &y;     *p = 3;     *q = 3.14;          increaseByOne(p, q);
 cout << x << " " << y << endl; cout << p << " " << q << endl; return 0; } ``` ``` void increaseByOne(int* &p1, double* q1) { (*p1)++; (*q1)++; p1++; q1++; } ```
No,
55
5. What is wrong with the following code? Fix all the errors. int p=int new; 
delete[] p;
int* p = new int; 
delete p; OR int * p = new int[5]; delete p[];
56
Write a code that generates random integers from 10 to 20.
``` #include #include ``` srand(time(0)); int = x; x = rand() % 11 + 10;
57
In the following code segment, what will be stored in the stack area of the program? int* m[3];
m[3];
58
In the following code segment, what will be stored in the heap area of the program? 
int* m[3];
for(int i = 0; i < 3; i++) m[i] = new int[4];
int[4]
59
What is wrong with the following code segment? Fix the error. int* m[3]; 
for(int i = 0; i < 3; i++) m[i] = new int[4]; 
 delete [] m;
int* m = new int[3]; for(int i = 0; i < 3; i++) m[i]; delete [] m;
60
Write a code that initializes a pointer variable pointing to an integer.
Int *p = &x;
61
What will be a problem with the following code segment? Fix the error(s). const int SIZE = 5; double * p = new int[SIZE]; for(int i = 0; o <= SIZE; i++) cout << (p + i) << endl;
const int SIZE = 5; int * p = new int[SIZE]; for(int i = 0; i <= SIZE; i++) cout << p[i] << endl;