CSCI 2270 Multiple Choice CU Boulder Midterm 1

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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? a 4 b None of these c 0 d 3 e 2

a

The syntax int &x is used in a function declaration when: a x is passed by reference b x is declared outside the function c None of above. d x is passed by value

a

What are the two necessary and fundamental components of a linked list node? a Data, and a pointer to the next node in the linked list sequence b Data, and a pointer to the last node in the linked list sequence c Data, and the data present in the neighboring nodes in the linked list sequence d Data, and a pointer to the first node in the linked list sequence e There are actually 3 necessary and fundamental components: data, a reference to the next node in the linked list sequence, and an index

a

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) a 0 b 1 c 2 d 3

a

What is the type of argv? a char * [] b string [] c int [] d int *

a

What is wrong with the following structure definition? myStruct { string name; float size; int id; } a There is a missing semicolon at the end of the definition and there is a missing struct type at the beginning b There is a missing semicolon at the end of the definition c There is nothing wrong with this definition d There cannot be variables of different types inside of one struct, all the attributes need to be the same type

a

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; } a Pass by reference b Pass by value c Pass by pointer d Pass by inheritance

a

Which of the following are true about linked lists? a All you need to start working with a linked list is a pointer to the first node in the list b Linked lists can only be up to 100 nodes long c All nodes in a linked list are adjacent in memory d It's impossible to insert nodes in the middle of a linked list e All of these are true

a

Which of the following is True about heap memory? a No limit on memory size b You must manage memory by allocating and freeing variables c Don't have to explicitly free variables d It is freed automatically when a function finishes

a

Which of these will print 1 to the command line? Select ALL that apply. int arr[10] = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0}; a std::cout << arr[0]; b std::cout << arr[9]; c std::cout << arr[-2]; d std::cout << *arr; e std::cout << arr[4] - *(arr+3);

a ,d and e

Which of the following are true about the queue abstract data type? Select all that apply. a It's possible for queues to be implemented using a linked list representation or an array representation b Queues behave in a "first in, first out" manner c It is impossible for queues to be implemented using a linked list representation d None of these are true e Queues behave in a "last in, first out" manner

a and b

What is wrong with the following array-based implementation of the push() operation on a stack? void push(int element) { data[top] = element; } a It does not check for the overflow condition b There is a memory leak c It does not increment the top variable d It does not check for the underflow condition

a and c

Which of the following variables are pointer variables? int * a , b; int* c, d; a b c d

a and c

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. a if(head->next == NULL) b if(head == NULL) c if(head->key == 0) d if(head == tail) e if (head->key==-1)

a and d

Which of the following are true about the number of elements in linked lists? a The size of a linked list is increased by 1 node size when a new node is added b The capacity of the linked list is doubled when the size reaches maximum capacity c The maximum size of a linked list is 4 GB d The size of a linked list is decreased by 1 node size when an existing node is deleted

a and d

Which of the following are true about the stack abstract data type? Select all that apply. a Stacks behave in a "last in, first out" manner b Stacks behave in a "first in, first out" manner c Stacks cannot be implemented using arrays d It is possible to implement a stack using a linked list representation or an array representation e Stacks cannot be implemented using linked lists

a and d

Suppose you have a Student struct defined as follows: struct student{ string name; string email; string birthday; }; Then, you write the following code in a properly defined int main (): student s; s.name = 'Iam Astudent'; s.email = '[email protected]'; s.birthday = '2 February 2002'; student *myStudent = &s; What is a valid way to make the program print "[email protected]"? Select all that apply: a std::cout << s.email <<std::endl; b std::cout << (&s)->email << std::endl; c std::cout << myStudent->email << std::endl; d std::cout << s->email << std::endl;

a, b and c

We have a deleteNode function which will delete the node with key equal to val. What's wrong with the function? Select all that apply. struct Node{ int key; Node *next; }; int deleteNode(int val){ Node* pres = head; Node* prev = NULL; while(pres->key != val) { prev = pres; pres = pres->next; } prev->next = pres->next; return 1; } a function assumes that val is somewhere in the list b function doesn't check edge case when head is NULL c nothing wrong d function doesn't free the space of the node to delete after adjusting prev->next pointer

a, b and d

Given the following code snippet, which of the following lines of code will cause a compiler error? int a = 10; int *c = &a; int *d; a c = 13; b c = a; c d = c; d d = &a; e *d = &a; f *c = 13;

a, b, and e

Consider a singly linked list implementation, but one where we have both a pointer to the head of the list (called head) and a pointer to the tail of the list (called tail). For this data structure, which of the following operations will require traversing the whole list? a Insert a new node after the last node b Delete the last node c Delete the first node d Insert a new node before the first node

b

Consider the following structure of a node for a linked list. struct Node { int key; Node *next; }; What will be the output of the following pseudo-code? Assume, head is a pointer to the first node of the following linked list. 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker = walker->next; } cout << walker->key << endl; a 30 b 50 c 10 d 20

b

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; a *b.feet b (*b).feet c b.feet d (*b)->feet

b

Imagine you have the following code: int main() { int old_array[10]; int *array = new int[10]; for (int i = 0; i < 10; i++) { old_array[i] = i; } for (int i = 0; i < 10; i++) { array[i] = old_array[i]; } array = old_array; std::cout << array[9] << std::endl; return 0; } What is the result of the following code if ran? a Everything is fine with this code, so it will just print '9' to the command line b There will be a memory leak c There is a syntax error so nothing will compile d Everything is fine with this code, so it will just print '1' to the command line

b

What do you need to keep track of throughout an algorithm that swaps two nodes x and y in a linked list? a 2; the pointer to x and the pointer to y b 4; the pointer to x and the pointer to y, as well as the pointer to the node before x and the node before y c 3; the pointer to x and y and the pointer to the head of the list d 1; the pointer to the head of the list

b

What is the output of the following function and function call? Cost is represented by float subTotal and the tax is represented by float tax. void CalculateCost(int count, float& subTotal, float taxCost) { if(count<10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; } int main() { float tax = 0.0, subTotal = 0.0; CalculateCost(15,subTotal,tax); cout<<"The cost for 15 items is"<<subTotal<<", and the tax for<<subTotal<<"is"<<tax<<endl; return 0; } a The cost for 15 items is 3.00, and the tax for 3.00 is 0.30; b The cost for 15 items is 3.00, and the tax for 3.00 is 0.00; c The cost for 15 items is 0.00, and the tax for 3.00 is 0.30; d The cost for 15 items is 0.00, and the tax for 3.00 is 0.00;

b

What should the first step of any dynamic array resizing function be? a assigning the old array pointer to the new array pointer b doubling the capacity c deleting the pointer to the old array d allocating a new array with the same size as the old array and copying all of the elements from the old array to the new array

b

Which line can free the space correctly? int* foo(int x){ int *y = new int[x]; return y; } int main( ) { int * z = foo(10); //free space ______________; return 0; } a delete z b delete [] z c delete *z d delete [] *z

b

Which of the following are true about the delete keyword? a You need to call delete every time you statically allocate memory. b To prevent memory leaks, every 'new' usage should be cleaned up with a 'delete' usage c There is no delete keyword d Arrays can be deleted using the 'delete {...}' syntax} e None of these are true

b

Which of the following would correctly read an entire line from an input file stream named f_input into a string variable named line? a f_input.getline(line,80); b getline(f_input, line); c f_input.getline(line); d line = getline(f_input);

b

Will the following code print the same output every time it's executed? char a = 'x'; char *b = &a; cout << a << " " << b << endl; a Yes b No

b

Which of the following is true about statically allocated circular array queues? a The circular array queue always uses more memory than a Linked List queue b One correct way to set the new tail pointer after enqueueing into a circular array is tail = (tail+1) % capacity c Circular array queues are last in, first out d The capacity of the queue is fixed e The index of the head is always equal to 0

b and d

Which of the following points are important when deleting a node B from a linked list A->B->C ? Select all that apply. a The value of the node B must be saved into the head of the linked list b The node A must be properly connected to the node C by using the next pointer c B->next should be set to NULL d The memory associated with the node B must be freed

b and d

The queue implementation based on circular arrays is more efficient than straightforward array-based implementation because a Both enqueue and deque operations are done in O(n) time complexity b Both enqueue and dequeue operations are done in O(1) time complexity c We don't need the tail variable for this implementation d We don't need the head variable for this implementation e There is no shifting of elements involved

b and e

Which of the following are true about classes and structs in c++? a Structs cannot hold other structs while classes can b Structs have destructors while classes do not c Classes hide implementation details from the end user by default while structs do not d Structs cannot have functions while classes can

c

Which of the following is true about memory allocation of linked lists? a Linked lists can only be created using stack memory b Linked lists can only be created using heap memory c Linked lists can be created using both stack memory and heap memory d None of these

c

Which of the following is true about the stack frame? a Each function call allocates a new stack frame b Variables allocated statically are deleted automatically when the stack frame returns c All of these are true d Stack frames live within the call stack e Variables allocated statically live inside their respective stack frame

c

Which of the following operations is performed more efficiently by doubly linked list than by linear linked list? a Searching an unsorted list for a given item b Traversing the list to print the contents of each node c Deleting a node whose location is given d Inserting a node after the node with a given location

c

#include<iostream> int main(){ int val = 0; int *ptr = new int; ptr = &val; val++; std::cout << "val = " << *ptr << std::endl; delete ptr; return 0; } a val = 1 b val = 0 c val = 0, followed by a crash at runtime d val = 1, followed by a crash at runtime

d

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); a This is caused by the line: ptr = ptr->next b This is caused by the line: cout << key; c This is caused by the line: while(ptr !=NULL && ptr->key !=key) d This is caused by the line: if (ptr->key==key)

d

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(1); q.enqueue(5); q.enqueue(6); q.dequeue(); q.enqueue(3); q.enqueue(4); q.enqueue(8); q.dequeue(); q.dequeue(); a 1,5,3 b The queue will be empty c 1,5,6 d 3,4,8 e 1,5,8

d

Given an implementation of stacks with linked lists, what is a better choice with respect to the complexity of push and pop operations? a Linked lists based implementations of stacks cannot avoid O(n) complexity from both pushes and pops. Use arrays instead. b Top points to the last element of the list c Both of them are equally efficient d Top points to the first element of the list

d

What is the time complexity (Big-O) for an optimized algorithm that searches a stack to see if it contains a value? a O(1) b Stacks cannot be searched c O(n^2) d O(n)

d

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. a Only A b Neither of these are true c Only B d Both of these are true

d

Which of the following are true about the new keyword? a Heap memory is not used when calling new. b If using new to allocate memory for a class, the constructor for that class is not called. c Memory allocated with new is automatically cleaned up by c++ garbage collection d None of these are true e All of these are true f Arrays declared with the new keyword have their memory allocated in the stack frame.

d

Which of the following is not a valid mode parameter when opening a file? a ios::in b ios::out c ios::binary d ios::ternary

d

Which of the following statements is true? Deallocating memory pointed to by the head of a Linked List always deallocates the memory used by whole linked list a Time complexity of inserting an element into a linked list is always O(1) b Deleting the tail node in a singly linked list doesn't need to traverse the linked list. c Assuming the node and the linked list is defined as below: 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; } ~LinkedList(); }; d None of them is true

d

Which statement is correct about pass-by-value parameters? a Parameters are always in read-write mode. b None of the other options are correct. c It can change the actual paramter value. d It cannot change the actual parameter value.

d

Assume you're given the following sentence, as well as a Stack class with push and pop operations. These push and pop operations push a word onto the stack and pop a word from the stack, respectively. Whenever you pop a word from the stack, that word is added to a Message Board and displayed. What is displayed on the Message Board after the following sequence of operations? Answer with words separated by spaces. push(Life) push(is) push(either) pop() pop() push(a) pop() push(daring) push(adventure) pop() pop() push(or) push(nothing) pop() a is a adventure daring either nothing b Life is either a daring adventure or nothing c either is a adventure daring or d nothing or adventure daring a either is Life e either is a adventure daring nothing

e


Set pelajaran terkait

Lecture 11 - Introducing & Naming New Products and Brand Extensions

View Set

human capital chapter 14, HR ch:9, Chapter 10, Chapter 12, ch 11 383

View Set

reading assignment: human Resource Management

View Set

CFE- Financial Transactions (Missed)

View Set