CSCI 675 - p2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What will the output in file data.txt after we execute the following Java program? Answers: nothing in file Hello World Compile error in line 5 HelloWorld Compile error in line 3

Compile error in line 3

What is the output of the following code? Answers: x = 10 y = 20 x = 20 y = 10 Compile error(s) in mySwap method Compile error(s) in main method

Compile error(s) in main method

Give the following simplified classes and code, what is the output of the last statement? class Pet{ public: virtual void print(); string name; private: }; class Dog:public Pet{ public: void print(); string breed; }; void Pet::print(){ cout << "My name is " << name; } void Dog::print(){ Pet::print(); cout << "My breed is " << breed << endl; } Dog* dPtr = new Dog; Pet* pPtr = dPtr; dPtr->name = "rover"; dPtr->breed = "Collie"; pPtr->print(); Answers: My name is roverMy breed is Collie My breed is Collie My name is rover My breed is Collie Nothing will be printed Response Feedback: The student shall understand the polymorphism

My name is roverMy breed is Collie

What is the output of the following Java program? Answers: Run time exception 0 MyClass@xxxxxxxx where xxxxxxxx is some hexadecimal number

MyClass@xxxxxxxx where xxxxxxxx is some hexadecimal number

Consider the Node structure as following /* Node of a doubly linked list */ struct Node { int data; Node *next; // Pointer to next node in DLL Node *prev; // Pointer to previous node in DLL }; What will be the code for step 2 in C++? /* Given a node as prev_node, insert a new node after the given node */ void insertAfter(struct Node* prev_node, int new_data) { /*1. check if the given prev_node is NULL */ /* 2. allocate new node */ /* 3. put in the data to the new node */ /* 4. Make next of new node as next of prev_node */ /* 5. Make the next of prev_node as new_node */ /* 6. Make prev_node as previous of new_node */ /* 7. Change previous of new_node's next node */ } Answers: Node new_node = new Node; Node *new_node = new Node; Node new_node; Node *new_node; Response Feedback: Students shall know basic algorithms (insert after, insert in front, back, and delete) about double linked list. May Study at http://www.geeksforgeeks.org/doubly-linked-list/

Node *new_node = new Node;

Which of the following is a true about Binary Trees Answers: Every binary tree is either complete or full. Every full binary tree is also a complete binary tree. Every complete binary tree is also a full binary tree. No binary tree is both complete and full None of the above Response Feedback: Students need to know all properties and algorithms of a binary tree, including pre-order, in-order, and post order traverse, Bread first search and Depth first search. May study at http://www.geeksforgeeks.org/binary-tree-data-structure/

None of the above

What is the run time of binary search? Answers: O left parenthesis ln space n right parenthesis O(1) O(n) O left parenthesis n squared right parenthesis Response Feedback: Students need to know the linear search and binary search and their running time

O left parenthesis ln space n right parenthesis

What is the worse case run time for following algorithm? MaximumSubsequenceSum Input: a1, a2,...,an n, the length of the sequence. Output: The value of the maximum subsequence sum. maxSum := 0 For i = 1 to n thisSum := 0 For j = i to n thisSum := thisSum + aj If ( thisSum > maxSum ), maxSum := thisSum End-for End-for Return( maxSum ) Answers: O left parenthesis 1 right parenthesis O left parenthesis n squared right parenthesis O left parenthesis n space log n right parenthesis O left parenthesis n right parenthesis Response Feedback: Student shall know big O notation. Also Big capital omega space a n d space B i g space capital theta. Be able to analyze the run time of a given algorithm.

O left parenthesis n squared right parenthesis

What is the time complexity of Build Heap operation. Build Heap is used to build a max(or min) binary heap from a given array. Build Heap is used in Heap Sort as a first step for sorting. Answers: O(log n) O(n^2) O(n) O(n log n) Response Feedback: Student shall know heap algorithms, properties, insert, and delete, and the run time. May study at http://www.geeksforgeeks.org/heap-data-structure/

O(n)

We use (Root, LeftSubTree, RightSubtree) notation to represent a tree. Assume that we have AVL tree (7, 5, (8, NULL, 9)), after insert new node 10, what will be the new AVL tree? Here NULL represent empty subtree. In this case, 8 has no left child. Answers: None of these (7, 5, (9, 8 10)) (7, 5, (8, NULL, (9, NULL, 10)) (8, (7, 5, NULL), (9, NULL, 10)) Response Feedback: Student shall know AVL insertion, rotation, and property. May study at http://www.geeksforgeeks.org/avl-tree-set-1-insertion/ May use to https://www.cs.usfca.edu/~galles/visualization/AVLtree.html visualize.

(7, 5, (9, 8 10))

What is the output of the following code? public class MyClass{ private int x; public void output() { System.out.println(x); } public static void main(String [] args) { MyClass myClass = new MyClass(); // 1 myClass.output(); // 2 } } Answers: Compile error at line // 1 Compile error at line // 2 0 garbage value

0

Which of the following statement(s) is TRUE? A hash function takes a message of arbitrary length and generates a fixed length code. A hash function takes a message of fixed length and generates a code of variable length. A hash function may give the same hash value for distinct messages. Answers: 1 only 2 only 1 and 3 only 2 and 3 only Response Feedback: Students need to know hash with open addressing and separate chaining. May study at http://www.geeksforgeeks.org/data-structures/

1 and 3 only

Prove that 1 cross times 2 plus 2 cross times 3 plus... plus n cross times left parenthesis n plus 1 right parenthesis equals fraction numerator n left parenthesis n plus 1 right parenthesis left parenthesis n plus 2 right parenthesis over denominator 3 end fraction If we proof by mathematical induction, what will be proven under certain condition in inductive step? Answers: 1 cross times left parenthesis 1 plus 1 right parenthesis equals fraction numerator 1 left parenthesis 1 plus 1 right parenthesis left parenthesis 1 plus 2 right parenthesis over denominator 3 end fraction 1 cross times 2 plus 2 cross times 3 plus... plus k cross times left parenthesis k plus 1 right parenthesis plus left parenthesis k plus 1 right parenthesis cross times left parenthesis k plus 2 right parenthesis space equals space fraction numerator left parenthesis k plus 1 right parenthesis left parenthesis k plus 2 right parenthesis left parenthesis k plus 3 right parenthesis over denominator 3 end fraction 1 cross times 2 plus 2 cross times left parenthesis 2 plus 1 right parenthesis space equals space fraction numerator 2 left parenthesis 2 plus 1 right parenthesis left parenthesis 2 plus 2 right parenthesis over denominator 3 end fraction k is positive integer and 1 cross times 2 plus 2 cross times 3 plus... plus k cross times left parenthesis k plus 1 right parenthesis equals fraction numerator k left parenthesis k plus 1 right parenthesis left parenthesis k plus 2 right parenthesis over denominator 3 end fraction Response Feedback: Student shall know how to proof by mathematical induction and use mathematical induction to prove recurrence relation.

1 cross times 2 plus 2 cross times 3 plus... plus k cross times left parenthesis k plus 1 right parenthesis plus left parenthesis k plus 1 right parenthesis cross times left parenthesis k plus 2 right parenthesis space equals space fraction numerator left parenthesis k plus 1 right parenthesis left parenthesis k plus 2 right parenthesis left parenthesis k plus 3 right parenthesis over denominator 3 end fraction

What is the output of the following piece of code? int i = 0; while(++i <= 5) cout << i << ", "; Answers: This is an infinity loop, nothing output 1, 2, 3, 4, 5, 6, There is a syntax error 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, Response Feedback: Students shall know the syntax and semantics of for loop, while loop, and do while loop.

1, 2, 3, 4, 5,

Suppose the input is 1234. What is the output of the following function? void display(int x){ if(x < 10){ cout << x; return; } display(x/10); cout << x%10 << ", "; } Answers: 12, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 12, 3, 4 Response Feedback: Students shall be able to write and trace recursive algorithm

12, 3, 4,

Consider the following C++ code struct node { int data; struct node *left, *right; }; void print(node *root, int k, int &count) { if (root != NULL && count <= k) { print(root->right, k, count); count++; if (count == k) cout << root->data; print(root->left, k, count); } } what is output of print(root, 3, count); where int count = 0; and root represent the following tree? Answers: 10 16 20 10 20 Response Feedback: Students shall be able to trace a binary search tree, do insert, delete algorithms, and their run time. May study at http://www.geeksforgeeks.org/binary-search-tree-data-structure/

16

What is the output? #include <iostream> #include <iterator> #include <list> using namespace std; int main () { list<int> mylist; for (int i = 0; i < 10; i++) mylist.push_back (i * 10); list<int> :: iterator it = mylist.begin(); advance (it, 5); cout << *it << endl; return 0; } Answers: 30 40 50 60 Response Feedback: Students shall know the functions in iterator class. May study http://www.cplusplus.com/reference/iterator/

50

Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler. #include<iostream> #include<stdlib.h> using namespace std; template<class T, class U, class V=double> class A { T x; U y; V z; static int count; }; int main() { A<int, int> a; A<double, double> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; } Answers: 20 28 16 24 There is a syntax error 8 16 Response Feedback: Student shall know how to write and use template functions and classes For more study, visit http://www.geeksforgeeks.org/c-plus-plus-gq/templates-gq/

16

What is the output of following function for start pointing to second node of following linked list? 1->2->3->4->5->6 void fun(struct node* start) { if(start == NULL) return; cout << start->data; if(start->next != NULL ) fun(start->next->next); cout << start->data; } Answers: 2 4 6 6 4 2 2 4 6 2 4 6 1 3 5 5 3 1 1 3 5 1 3 5 Response Feedback: Students shall know the linked list and how to implement it. May study at http://www.geeksforgeeks.org/data-structures/linked-list/

2 4 6 6 4 2

Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2) Preorder 3) Postorder Answers: 1 and 3 2 and 3 1 and 2 all of three Response Feedback: Students shall be able to trace a binary search tree, do traversal, insert, delete algorithms, and their run time. May study at http://www.geeksforgeeks.org/binary-search-tree-data-structure/

2 and 3

Assume that x has prime factorization: 3 squared cross times 5 cross times 7 cubed, and y has prime factorization: 2 cubed cross times 5 squared cross times 7 squared, what is the least common multiple of x and y? Answers: 5 cross times 7 squared 2 cubed cross times 3 squared cross times 5 to the power of 7 x 7 to the power of 5 2 cubed cross times 3 squared cross times 5 squared cross times 7 cubed 2 cubed cross times 3 squared cross times 5 cubed cross times 7 to the power of 5 Response Feedback: Student shall know the concept about prime factorization, greatest common divisor and least common multiple. Also know that factoring is a hard problem

2 cubed cross times 3 squared cross times 5 squared cross times 7 cubed

What is the output of the following code? vector<string> names; names.push_back("Tom"); names.push_back("Jack"); cout << names.size() << ", " << names[1] << endl; Answers: 2, Tom 10, Jack 2, Jack There is a syntax error Response Feedback: Students shall know string and vector and their member functions. Study them at http://www.cplusplus.com/reference/

2, Jack

What is the output of the following code? Answers: 20 There is a syntax error in line 12 There is a syntax error in line 10 15

20

Given array {3, 7, 6, 5, 2, 1, 8, 4}, use quick sort algorithm to sort the array. what will be the return value from the first call of partition? Assume the last element is used as pivot for partition and array index starts from 0. Answers: 6 5 4 3 Response Feedback: Students shall understand the quick sort, merge sort, insertion sort, and selection sort. Also know their run time and the stability. May study at http://www.geeksforgeeks.org/

3

What is the output of the following Java program? Answers: -1 There is compile error 4 There is run time error since the array to be searched is not sorted

4

A bit string consists of 0s and 1s. For example, 0101 is a bit string with four bits. How many bits strings with length 6 or 5 that begins with 0? Answers: 48 64 32 24 Response Feedback: Student shall know sum and product rules, permutation and combination, and the inclusive and exclusive rule

48

Suppose that I have function void myswap(int x, int y){ int temp = x; x = y; y = temp; } what will be the output of the following code segment? int x = 5, y = 10; myswap(x, y); cout << x << ", " << y; Answers: 5, 10 There is a run time error 10, 5 There is a syntax error Response Feedback: Students shall be able to write and use functions with pointer argument and can write and call functions that pass by reference

5, 10

What is the multiplicative inverse of 6 Mod 7? Answers: 5 1 3 6 Response Feedback: Student needs to know Euclidean Algorithm, Extended Euclidean Algorithm, and how to calculate the multiplicative inverse

6

What will be the output of the code? Assume user types 5 on keyboard? int *p, x; p = new int; *p = 10; cin >> x; p = &x; cout << ++*p; Answers: 5 11 There is a syntax error 6 10 Response Feedback: Student shall know how to declare a pointer variable. Allocate memory, How to initialize the value pointed by it and How to use the pointer variable.

6

Assume int x = 15; int y = 10; What will be the y value after the execution of following C++ code segment? if(x > y) if(x > 20) y = 6; else y = 7; Answers: There is a syntax error 7 10 6 Response Feedback: Students shall pay attention to the syntax of if, if-else, if-esel if -else, and switch structure

7

To proof that 7 to the power of n space end exponent minus space 1 space i s space d i v i s i b l e space b y space 6 space f o r space a l l space p o s i t i v e space i n t e g e r space n. We want to prove by mathematical induction. Which of the following is in induction step? Answers: 7 to the power of n plus 1 end exponent minus 1 equals 7 left parenthesis 7 to the power of n minus 1 right parenthesis space space plus space 6 7 to the power of n minus 1 space equals space 7 left parenthesis 7 to the power of n minus 1 end exponent minus 1 right parenthesis plus 6 When n = 1, the statement is true W h e n space n space equals space 2 comma space 7 squared minus 1 space equals space 48 space i s space d i v i s i b l e space b y space 6 Response Feedback: Student shall know five proof skills. Direct proof, proof by contradiction, proof by cases, proof by contrapositive, and proof by mathematical indcution

7 to the power of n plus 1 end exponent minus 1 equals 7 left parenthesis 7 to the power of n minus 1 right parenthesis space space plus space 6

A large intersection left parenthesis A large union B right parenthesis equals Answers: B A A large union B A large intersection B Response Feedback: Student shall know set operations and set identities

A

What is the output of the following code? Answers: Derived::fun() called Compile error in line 8 Base::fun() called Compile error in line 16

Base::fun() called

Which of the following statements is/are TRUE for an undirected graph? P: Number of odd degree vertices is even Q: Sum of degrees of all vertices is even Answers: P only Q only Both P and Q Neither P or Q Response Feedback: Student needs to know types of graph, the representation of a graph, the algorithms related to the graph. May study at http://www.geeksforgeeks.org/graph-data-structure-and-algorithms/

Both P and Q

Which of the following return true if char c is an English letter. Assume that c has been initialized. Answers: Character.isAlpha(c) Character.isLetter(c) Character.isLetterOrDigit(c) Character.isDigit(c)

Character.isLetterOrDigit(c)

What is true about the following Java program Answers: There is a compile warning message indicates that one catch clause can never be reached. However, the program still can run and the output will be: Caught derived class exception Compile error in line 9 Compile error in line 12. There is no compile warning or error. The output will be: Caught derived class exception

Compile error in line 12.

Which of the following algorithms can be used to most efficiently determine the presence of a cycle in a given graph ? Answers: Kruskal' Minimum Spanning Tree Algorithm Breadth First Search Prim's Minimum Spanning Tree Algorithm Depth First Search Response Feedback: Student needs to know types of graph, the representation of a graph, the algorithms related to the graph. May study at http://www.geeksforgeeks.org/graph-data-structure-and-algorithms/

Depth First Search

Is the following statement valid about shortest paths? Given a graph, suppose we have calculated shortest path from a source to all other vertices. If we modify the graph such that weights of all edges is becomes double of the original weight, then the shortest path remains same only the total weight of path changes. Answers: False Response Feedback: Student needs to know types of graph, the representation of a graph, the algorithms related to the graph. May study at http://www.geeksforgeeks.org/graph-data-structure-and-algorithms/

False

What is the output of the following Java code segment? StringBuilder s = new StringBuilder("Hello"); s.replace(1, 7, "World"); //1 System.out.println(s); Answers: HWorldo Compile error in line //1 HWorld StringIndexOutOfBoundsException at line //1

HWorld

What is the output of the following code if user typed: Hello World then hit Enter Key? Scanner input = new Scanner(System.in); String greeting = input.nextLine(); System.out.println(greeting); Answers: There is run time error There is a syntax error Hello Hello World

Hello World

What is the output? #include <iostream> using namespace std; int main() { try { try { throw 10; } catch (int n) { cout << "Inside Catch\n"; throw; } } catch (int a) { cout << "Outside Catch\n"; } return 0; } Answers: There is a syntax error Inside Catch Inside Catch Outside Catch Outside Catch Response Feedback: Students shall know how exception is handled. For extra study, may visit http://www.geeksforgeeks.org/c-plus-plus-gq/exception-handling-gq/

Inside Catch

Which of the following is true about pure virtual functions? 1) Their implementation is not provided in a class where they are declared. 2) If a class has a pure virtual function, then the class becomes abstract class and an instance of this class cannot be created. Answers: Both 1 and 2 are correct Only 1 is correct Only 2 is correct Neither 1 nor 2 is correct Response Feedback: Students shall understand virtual function, pure virtual function, and abstract class. May study https://en.wikipedia.org/wiki/Virtual_function

Only 2 is correct

Consider the following implementation, what is the correct? int mystrcmp(const char* str1, const char * str2){ while(*str1 != '\0' && *str2 != '\0') if(*str1++ != *str2++) break; return *str1 - *str2; } Answers: This implementation is correct. It will compare two strings as built-in strcmp function does This implementation is incorrect. The two arguments shall not be const char * This implementation is incorrect since there will be infinite loop This implementation is incorrect since there are some syntax error Response Feedback: students shall understand built-in C-string functions and know how to implement them

This implementation is correct. It will compare two strings as built-in strcmp function does

Which of the following are propositions? Answers: Give me a cup of tea, please Would you like to have a cup of tea? He likes to drink tea. This tea tastes good.

This tea tastes good.

What is the output of the following code if the value of score is 78? Assume variable score and grade has been declared as int and char, respectively. Answers: U There is a compile error in line 12 C There is a compile error in line 11

U

What is the output of the following Java program? Answers: Compile error in line 23 Compile error in line 17 a = 20 a = 10

a = 20

Given characteristic equation for a recurrence relation. Express the solution to recurrence relation as a linear combination of terms. (x-1)(x+2) = 0 Answers: 1 plus left parenthesis minus 2 right parenthesis to the power of n a subscript 1 plus a subscript 2 left parenthesis minus 2 right parenthesis to the power of n a subscript 1 left parenthesis minus 1 right parenthesis to the power of n plus a subscript 2 2 to the power of n left parenthesis minus 1 right parenthesis to the power of n plus 2 to the power of n Response Feedback: Student needs to know how to solve linear homogeneous recurrence relation

a subscript 1 plus a subscript 2 left parenthesis minus 2 right parenthesis to the power of n

Which of the following declare and initialize a character type variable name grade with value 'A'? Answers: char grade = "A" char grade = A; char grade = 'A'; char grade = "A"; Response Feedback: The students shall know how to correctly declare and initialize variables of type int, char, double, and bool

char grade = 'A';

Which of the following Boolean function represents the following circuit? Answers: f left parenthesis x comma space y comma space z right parenthesis equals top enclose x space plus space y space plus top enclose y z f left parenthesis x comma space y comma space z right parenthesis space equals space left parenthesis top enclose x plus y right parenthesis top enclose x z f left parenthesis x comma space y comma space z right parenthesis space equals space left parenthesis top enclose x z space plus top enclose y right parenthesis z f left parenthesis x comma space y comma space z right parenthesis space equals space top enclose left parenthesis top enclose x plus y right parenthesis end enclose space plus space y z (There is a short bar on top of x) Response Feedback: Students need to know the relationship between Boolean function and circuit

f left parenthesis x comma space y comma space z right parenthesis space equals space left parenthesis top enclose x plus y right parenthesis top enclose x z

What is miss from the outer loop head in following insertion sort algorithm? void insertionSort(int arr[], int n) { int i, key, j; for (______________) { key = arr[i]; j = i-1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; } } Answers: i = 1; i < n - 1; i++ i = 1; i < n; i++ i = 0; i < n - 1; i++ i = 0; i < n; i++ Response Feedback: Student shall be able to trace and implement insertion sort and selection sort algorithm. May study at http://www.geeksforgeeks.org/

i = 1; i < n; i++

If the name of the input file was in a variable named filename, which of the following is the correct way to open the input stream named inFile and associate it with this file? Answers: inFile = filename; inFile.open("filename"); inFile.open(filename); inFile = "filename"; Response Feedback: Student shall know how to open file to read or write and handle the situation that file cannot be open successfully.

inFile.open(filename);

To overload functions with symbolic names (like + - / <<), you must use the keyword ________ before the symbolic name. Answers: operator const reference void Response Feedback: The student shall know how to oeverload operators +, -, *, /, >>, and <<. if it is needed

operator

What does the following function do? void fun(int n) { queue<int> q; q.push(0); q.push(1); for (int i = 0; i < n; i++) { int a = q.front(); q.pop(); int b = q.front(); q.pop(); q.push(b); q.push(a + b); cout << a << ", "; } } Answers: print out first n Fibonacci numbers reversely print out numbers 0 to n-1 reversely print out first n Fibonacci numbers print numbers 0 to n-1 Response Feedback: Student need to know the algorithm, implementation, and application for stack and queue. May study at http://www.geeksforgeeks.org/data-structures/

print out first n Fibonacci numbers

Which of the following is the correct head of main function in java? Answers: public static void main(String args) public static void main(String [] args) public static int main(String [] args) public void main(String [] args) Response Feedback: Student shall know the head of main function, which is an entry point of a project

public static void main(String [] args)

Suppose we have Random rand = new Random(); Which of the following expression generates a random integer between 20 and 100, inclusive? Answers: rand.nextInt() % 81 rand.nextInt() % 80 rand.nextInt() % 81 + 20 rand.nextInt() % 80 + 20 Response Feedback: Student shall know how to use random method in Math class and nextInt, nextDouble methods in Random class to generate random numbers in give range.

rand.nextInt() % 81 + 20

Which of the following access a variable named next in structure *student ? Answers: student->next student'next student student-next Response Feedback: know how to write and use struct

student->next

Which boolean function derived from following table? x y f(x, y) 0 0 1 0 1 0 1 0 0 1 1 1 Answers: y x y space plus space top enclose x y top enclose x y space plus space top enclose y x x y space plus space top enclose x space top enclose y Response Feedback: Student shall know basic Boolean operations, Boolean functions, and gates

x y space plus space top enclose x space top enclose y

Which of the following is the head of destructor of Employee class? Answers: ~Employee(Employee& other) ~Employee(const Employee& other) void ~Employee() ~Employee() void ~Employee(const Employee& other) void ~Employee(Employee& other) Response Feedback: Students are required to know how to write big three: Copy constructor, Destructor, and Assignment Operator for a class

~Employee()

Which one of these first-order logic formula is valid? Answers: ∀x∃y P(x, y) => ∃y∀x P(x, y) ∃x(P(x) ∨ Q(x)) => (∃xP(x) => ∃xQ(x)) ∃x(P(x) ∧ Q(x)) <=> (∃xP(x) ∧ ∃xQ(x)) ∀x(P(x) => Q(x)) => (∀xP(x) => ∀xQ(x)) Response Feedback: Study the Quiz questions and understand the answers http://www.geeksforgeeks.org/propositional-and-first-order-logic-gq/

∀x(P(x) => Q(x)) => (∀xP(x) => ∀xQ(x))


संबंधित स्टडी सेट्स

Topic 7: Motivation: Needs Theory & Expectancy Theory

View Set

integrated science a - unit 1: exam

View Set

Microbiology DNA Transcription to RNA Key Enzymes

View Set