Looks like no one added any tags here yet for you.
Assume that main() in driver.cpp uses a function defined in another file xyz.cpp.
g++ -std=c++11 driver.cpp xyz.cpp -o func
the correct way to close a file stream named oFile that is being used to output to a file with the name "filename.txt"?
oFile.close();
What type is argv?
array of character pointers
What is accessing an element outside the legal set of indices referred to?
double values[10];
values[10] = 5.4;
cout << values[10];
A segmentation fault
Select the constructor that will be called when the following program is executed.
#include <iostream>
#include <fstream>
using namespace std;
class FileReader {
char _sep = ','; // Separates cells in a line.
char _lineSep = ','; // Separates lines from one another.
string _fName; // Name of file to be parsed.
ifstream _fin;
public:
FileReader(string fName) {
_fName = fName;
}
FileReader(string fName, char sep, char lineSep) {
_fName = fName;
_sep = sep;
_lineSep = lineSep;
}
FileReader(char fName[], char sep, char lineSep) {
_fName = (string) fName[];
}
FileReader(string fName, string sep, string lineSep) {
// Convert seps to chars.
}
void readFile() {
// File Reading Goes Here.
_fin.open(_fName);
}
};
int main(int argc, char *argv[])
{
string fileName = argv[1];
FileReader reader = FileReader(fileName, '\t', '\n');
}
FileReader(string fName, char sep, char lineSep)
correctly read an entire line from an input file stream named f_input into a string variable named line?
getline(f_input, line);
What is wrong with the following structure definition?
myStruct{
string name;
float size;
int id;
}
There is a missing semicolon at the end of the definition and there is a missing struct type at the beginning
Using the structures below, how can we display the student's birth year?
struct calendar
{
int d;
int m;
int y;
};
struct student
{
int age;
float weight;
calendar bday;
};
student s;
}
cout << s.bday.y;
Which constructor will be called in the following program?
#include <iostream>
#include <fstream>
using namespace std;
class CSVReader {
char _sep = ','; // Separates cells in a line.
char _lineSep = ','; // Separates lines from one another.
string _fName; // Name of file to be parsed.
ifstream _fin;
public:
CSVReader(string fName) {
_fName = fName;
}
CSVReader(string fName, char sep, char lineSep) {
_fName = fName;
_sep = sep;
_lineSep = lineSep;
}
CSVReader(char fName[], char sep, char lineSep) {
_fName = (string) fName[];
}
CSVReader(string fName, string sep, string lineSep) {
// Convert seps to chars.
}
void readFile() {
// File Reading Goes Here.
_fin.open(_fName);
}
};
int main(int argc, char *argv[])
{
string fileName = argv[1];
CSVReader reader = CSVReader(fileName, '\t', '\n');
}
CSVReader(string fName, char sep, char lineSep)
Suppose your main function is the following:
int main(int argc, char *argv[]){
std :: cout << argc << std :: endl;
return 0;
}
Then, you compile it to the executable file named a.out.
If you run then run the following command: ./a.out arg1 arg2 arg3
What will print to the terminal?
4
Given the following code snippet, which of the following is equivalent to a.feet?
struct Distance {
int feet;
int inch;
};
Distance a;
Distance *b = &a;
(*b).feet
#include <iostream>
using namespace std;
struct Person
{
int weight ;
int height;
};
int main()
{
Person fred;
Person* ptr = &fred;
fred.weight=190;
fred.height=70;
return 0;
}
What is a correct way to subtract 5 from fred's weight using the pointer variable 'ptr'?
ptr->weight = ptr->weight - 5;
#include <iostream>
using namespace std ;
void test( int a){
a = a*3;
cout << &a << endl;
}
int main (){
int a = 15 ;
test( a );
cout << &a << endl;
}
Variables 'a' in main function and 'a' in test function are using the same memory location, True or False?
False
What is the output of the following code?
char a = 'q';
char *b = &a;
int c = 65;
int *d = &c;
cout << sizeof(d) - sizeof(b) << endl;
(Note: Assume that the size of char is 1 byte and the size of int is 4 bytes)
0
Which one is a correct way to print the fourth element of this array ?
int a[] = {1, 3, 5, 7};
cout << ____ << endl;
*(a+3)
Select the solution to replace the ? is in the main() if we want to free the memory allocated for all of the nodes?
//Destructor
FreeList::~FreeList()
{
Node *ptr = head;
while (ptr != NULL)
{
Node *cur;
cur = ptr;
ptr = ptr->next;
delete cur;
}
}
int main() {
FreeList *mylist = new FreeList();
//Free memory allocated for all nodes
______?______
return 0;
}
delete mylist;
How do you call this function?
int* var = getArray();
By the final line of this program, which of the following are true?
pb points to the same address as pa
c == 5
What should the first step of any dynamic array resizing function be?
doubling the capacity
Which line can free the space correctly?
int* foo(int x){
int *y = new int[x*x];
return y;
}
int main(){
int * z = foo(10);
//free space
______________;
return 0;
}
delete [] z;
Suppose a struct and function are defined as
struct sStruct{
int A;
int B;
};
void swap( sStruct*& op1 , sStruct*& op2 ){
sStruct* temp = op1;
op1 = op2;
op2 = temp;
}
Suppose the following code in main
:
sStruct* ptr1 = new sStruct{ 1 , 2 };
sStruct* ptr2 = new sStruct{ 3 , 5 };
swap( ptr1 , ptr2 );
Through pass-by-reference, ptr1
and ptr2
were modified.
What type of parameter passing is this?
#include <iostream>
using namespace std;
void add(int &num){
num = num + 10;
}
int main(){
int a = 10;
add(a);
cout << a << endl;
return 0;
}
pass by reference
Which function parameter list could make the program work and swap output ?
(int &v1, int &v2)
What type of argument passing does this code display?
#include <iostream>
using namespace std;
void add10 (int num){
num = num + 10;
}
int main (){
int a = 10;
add10(a);
cout << a << endl;
}
pass by value
What kind of parameter passing is this?
#include <iostream>
using namespace std;
void add5(int *b){
*b = *b + 5;
}
int main (){
int b = 8;
add5(&b);
cout << b << endl;
}
Pass by pointer
When traversing a singly linked list, we access each node using:
pointers
What are the two necessary and fundamental components of a linked list node?
Data, and a pointer to the next node in the linked list sequence
Assume the nodes in your linked list are structs with the following form:
struct Node{
int key;
Node *next;
};
And you have a function for searching the list for a specific key, and printing the key if it is found:
Node* LinkedList::searchList(int key) {
Node* ptr = head;
while (ptr != NULL && ptr->key != key)
{
ptr = ptr->next;
}
if (ptr->key == key){
cout << key;
}
return ptr;
}
If the linked list, linked, has the following values:
1->2->3->4->NULL
Why does the following call result in a Segmentation fault?
linked.searchList(8);
This is caused by the line: if (ptr->key==key)
Consider the following linked list:
How would you correctly insert the node B between A and C? Note that prev is a pointer to A and curr is a pointer to B.
Option 1:
prev->next = curr;
curr->next = prev->next;
Option 2:
curr->next = prev->next;
prev->next = curr;
Option 3:
curr->next = prev;
prev->next = curr;
Option 4:
prev->next = curr;
curr->next = prev;
Option 2
For following Linked List, we need to delete the node with Data value 4 from it with pointer prev and pres. We set pointes prev to point to NULL and pres to point to the head of the list at first as shown.
Then traverse the Linked List so that pres always points to the next node of the node prev points to. For example, after one iteration, prev points to the head of the Linked List and pres points to head->next. When we delete pres, what's the value of prev->Data?
3
Consider the following structure of a node for a linked list.
struct Node {
int key;
Node *next;
};
class LinkedList
{
private:
Node *head;//always points to the first node
Node *tail;//always points to the last node
public:
LinkedList(){
head = NULL;
}
void insert(Node *prev, int newKey);
Node* searchList(int key);
bool deleteAtIndex(int index);
void printList();
~LinkedList();
};
How can you check whether the linked list has only one node? Select all that apply.
if(head->next == NULL)
if(head == tail)
I want to delete a node from a linked list. Suppose curr is a pointer to the node I want to delete and prev is a pointer to the node before the node I want to delete. See the diagram:
How would you properly delete the node pointed to by curr?
prev->next = curr->next;
delete curr;
A Stack is a Last-In First-Out (LIFO) Abstract Data Type.
True
A Stack is a First In First Out (FIFO) Abstract Data Type.
False
What is wrong with the following array-based implementation of the push() operation on a stack?
void push(int element) {
data[top] = element;
}
It does not check for the overflow condition
It does not increment the top variable
Stack size =4
Perform the following sequence of operations on the stack and stop executing commands once the stack is full. What are the final contents of the stack from top to bottom?
push(8)
push(3)
pop()
push(7)
push(0)
pop()
pop()
push(2)
pop()
push(6)
push(10)
pop()
push(5)
push(9)
push(1)
9, 5, 6, 8
We have a circular queue implemented with a fixed size (10) array. For this queue, int head and int tail representing where we are going to delete and add the element respectively. If head is 9 and tail is 1, which of the following statement is correct after calling dequeue.
the head will be 0.
Which of the following are true about linked list implementations of a queue?
A: In the enqueue operation, if new nodes get inserted at the end of the list, then in the dequeue operation, nodes must be removed from the beginning of the list.
B: In the enqueue operation, if new nodes get added at the beginning of the list, then in the dequeue operation, nodes must be removed from the end of the list.
Both are true
Assume we have a queue named q. What will be the values stored in q after the following operations from front to rear?
q.enqueue(22);
q.enqueue(70);
q.enqueue(9);
q.dequeue();
q.dequeue();
q.enqueue(31);
q.enqueue(42);
q.dequeue();
q.enqueue(18);
31, 42, 18
Run time of indexing into an array
O(1)
Run time of indexing into a linked list
O(n)
Run time of insertinf a new value at the beginning of a linked list
O(1)
Run time of inserting a new value at the beginning of an array
O(n)
Run time of searching an array
O(n)
Run time of searching a linked list
O(n)
Run time of inserting a node in the middle of a linked list
O(n)
Run time of deleting from the end of an array
O(1)
Run time of an optimized algorithm that searches a stack to see if it contains a value
O(n)
Where would the node 0016 be inserted in the following BST?
0015 -> right
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16.
What is the height of the binary search tree?
3
The following tree is a valid binary search tree, True or False?
0002
/ \
0001 0003
\
0007
/ \
0004 0016
/ \
0012 0023
True
Which of the following values the variable A can take to keep the following tree a BST? Select all that apply.
9
22
27
14
18
10
/ \
5 A
/ \ / \
3 6 17 24
22 and 18
Given the below binary search tree, what is the order that nodes are evaluated in searching for 0012?
0002
/ \
0001 0003
\
0007
/ \
0004 0016
/ \
0012 0023
0002, 0003, 0007, 0016, 0012
Which nodes are ancestors of node 5? You may select more than one answer.
1 and 2
Using InOrder Traversal, in which order would the following tree be traversed?
4,2,5,1,6,3,7
What will be the output of the program?
1 2 1 3 1 2 1
Which of the following are true of a recursive function? Select all that apply.
The function must call itself
The function must maintain the index of the iteration in the recursion
The function must return something when it is first called
There must be a base condition to stop the recursion
The function must call itself
There must be a base condition to stop the recursion
You have been given a recursive function to sum the values of all keys in a binary tree:
int treeSum(TreeNode * root, int depth)
{
if(root != NULL)
{
int leftSum = treeSum(root->left, depth + 1);
int rightSum = treeSum(root->right, depth + 1);
cout << root->key;
if (depth != 0)
{
cout << " ";
}
return leftSum + rightSum + root->key;
}
else {
return 0;
}
}
Consider the following binary tree:
3
/ \
19 17
/ /
8 6
/ \
2 4
/
9
The following code is called:
treeSum(root, 0);
the key of root pointer is 3. Please select all correct output from cout?
17 is printed before 19
3 is printed last
8 is printed before 4
4 is printed before 8
4 is printed before 8
3 is printed last