Midterm 1 CSCI 2270

studied byStudied by 12 people
5.0(1)
Get a hint
Hint

Assume that main() in driver.cpp uses a function defined in another file xyz.cpp. 

1 / 58

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

59 Terms

1

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

New cards
2

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();

New cards
3

What type is argv?

array of character pointers

New cards
4

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

New cards
5

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)

New cards
6

correctly read an entire line from an input file stream named f_input into a string variable named line?

getline(f_input, line);

New cards
7

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

New cards
8

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;

New cards
9

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)

New cards
10

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

New cards
11

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

New cards
12

New cards
13
#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;

New cards
14
#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

New cards
15

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

New cards
16

Which one is a correct way to print the fourth element of this array ?

int a[] = {1, 3, 5, 7};

cout <<  ____  << endl;

*(a+3)

New cards
17

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;

New cards
18

How do you call this function?

15.png

int* var = getArray();

New cards
19

By the final line of this program, which of the following are true?

7.png

pb points to the same address as pa

c == 5

New cards
20

What should the first step of any dynamic array resizing function be?

doubling the capacity

New cards
21

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;

New cards
22

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.

New cards
23

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

New cards
24

Which function parameter list could make the program work and swap output ?

8.png

(int &v1, int &v2)

New cards
25

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

New cards
26

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

New cards
27

When traversing a singly linked list, we access each node using:

pointers

New cards
28

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

New cards
29

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)

New cards
30

Consider the following linked list:

17.png

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

New cards
31

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. 

19.png

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?

20.png


3

New cards
32

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)

New cards
33

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:

16.png

How would you properly delete the node pointed to by curr?


prev->next = curr->next;

delete curr;

New cards
34

A Stack is a Last-In First-Out (LIFO) Abstract Data Type.

True

New cards
35

A Stack is a First In First Out (FIFO) Abstract Data Type.

False

New cards
36

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

New cards
37

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?

  1. push(8)

  2. push(3)

  3. pop()

  4. push(7)

  5. push(0)

  6. pop()

  7. pop()

  8. push(2)

  9. pop()

  10. push(6)

  11. push(10)

  12. pop()

  13. push(5)

  14. push(9)

  15. push(1)

9, 5, 6, 8

New cards
38

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.

New cards
39

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

New cards
40

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

New cards
41

Run time of indexing into an array

O(1)

New cards
42

Run time of indexing into a linked list

O(n)

New cards
43

Run time of insertinf a new value at the beginning of a linked list

O(1)

New cards
44

Run time of inserting a new value at the beginning of an array

O(n)

New cards
45

Run time of searching an array

O(n)

New cards
46

Run time of searching a linked list

O(n)

New cards
47

Run time of inserting a node in the middle of a linked list

O(n)

New cards
48

Run time of deleting from the end of an array

O(1)

New cards
49

Run time of an optimized algorithm that searches a stack to see if it contains a value

O(n)

New cards
50

Where would the node 0016 be inserted in the following BST?

23.png

0015 -> right

New cards
51

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

New cards
52

The following tree is a valid binary search tree, True or False?

 

                              0002
                            /           \
                      0001       0003
                                               \   
                                             0007
                                             /         \
                                       0004         0016

                                                       /          \

                                                 0012        0023

True

New cards
53

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

New cards
54

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

New cards
55

Which nodes are ancestors of node 5? You may select more than one answer. 

22.png

1 and 2

New cards
56

Using InOrder Traversal, in which order would the following tree be traversed?

30.png

4,2,5,1,6,3,7

New cards
57

What will be the output of the program?

28.png

1 2 1 3 1 2 1

New cards
58

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

New cards
59

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

New cards

Explore top notes

note Note
studied byStudied by 5 people
... ago
5.0(1)
note Note
studied byStudied by 34 people
... ago
5.0(2)
note Note
studied byStudied by 10 people
... ago
5.0(2)
note Note
studied byStudied by 7 people
... ago
5.0(2)
note Note
studied byStudied by 113 people
... ago
4.5(2)
note Note
studied byStudied by 64 people
... ago
5.0(2)
note Note
studied byStudied by 21 people
... ago
5.0(1)
note Note
studied byStudied by 94 people
... ago
5.0(1)

Explore top flashcards

flashcards Flashcard (138)
studied byStudied by 2 people
... ago
5.0(1)
flashcards Flashcard (47)
studied byStudied by 96 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 12 people
... ago
5.0(1)
flashcards Flashcard (121)
studied byStudied by 8 people
... ago
5.0(1)
flashcards Flashcard (122)
studied byStudied by 35 people
... ago
5.0(2)
flashcards Flashcard (26)
studied byStudied by 6 people
... ago
5.0(1)
flashcards Flashcard (106)
studied byStudied by 7 people
... ago
5.0(1)
flashcards Flashcard (83)
studied byStudied by 231 people
... ago
5.0(2)
robot