Exam - USNA [PDF]

Sample. Final Written Examination. IC210. Closed Book, 3 Hours. Precedence Table. Which of the following is NOT a valid identifier (i.e. CANNOT be used as a name for a variable or function, etc.). number_of_elements; int; ARRAYSIZE; num2; All are valid. A compiler will catch which of the following types of programming ...

3 downloads 12 Views 164KB Size

Recommend Stories


2018 USNA SHIP LIST
Learning never exhausts the mind. Leonardo da Vinci

PdF The PMP Exam
I want to sing like the birds sing, not worrying about who hears or what they think. Rumi

PdF The PMP Exam
And you? When will you begin that long journey into yourself? Rumi

Cisa exam questions pdf
If you feel beautiful, then you are. Even if you don't, you still are. Terri Guillemets

PDF CMA Exam Preparation
Be grateful for whoever comes, because each has been sent as a guide from beyond. Rumi

[PDF] Exam 98-366
We must be willing to let go of the life we have planned, so as to have the life that is waiting for

Exam Booklet (PDF)
Don’t grieve. Anything you lose comes round in another form. Rumi

PDF Medical Assistant Exam
Don't be satisfied with stories, how things have gone with others. Unfold your own myth. Rumi

[PDF] The PMP Exam
Before you speak, let your words pass through three gates: Is it true? Is it necessary? Is it kind?

[PDF] PMP Exam Cram
If your life's work can be accomplished in your lifetime, you're not thinking big enough. Wes Jacks

Idea Transcript


Sample Final Written Examination IC210 Closed Book, 3 Hours Precedence Table

1. Which of the following is NOT a valid identifier (i.e. CANNOT be used as a name for a variable or function, etc.). a. number_of_elements b. int c. ARRAYSIZE d. num2 e. All are valid 2. A compiler will catch which of the following types of programming errors? a. syntax errors b. run_time errors c. logic errors d. All of the above e. None of the above 3. What is the value of product after the following statements? double x = 0, y = 9, product; x * y = product; a. 9.0 b. 0.0 c. 90.0 d. syntax error (explain): cannot have an expression (x*y) on the left hand side of an assignment statement . 4. Which of the following choices will NOT legally get 2 values from the user and assign them to the variables num1 and num2? a. cin >> num1; cin >> num2; b. cin >> num1 >> num2; c. cin >> num1 >> num2; d. cin >> num1, num2; 5. What is the output of the following code fragment? char x, y, z; x = ‘y’; y = ‘z’; z = x; cout << x << y << z << ‘z’; a. xyzz b. yzxz c. yzyz d. yzzz e. xyyz 6. What is the value of the variable result after the following code fragment? double result, n1 = 6, n2 = 4, n3 = 2; result = n1 / (n2 * n3); a. 0.0 b. 0.75 c. 2.0 d. 3.0 e. Garbage (result was not initialized) 7. What will be output to the screen in the following code fragment? int x = 20; if (x <= 20) { cout << “True ”; } cout << “Goodbye”; a. True Goodbye b. True c. Goodbye d. Nothing is output to the screen e. Syntax error (missing the else) 8. Which of the following is NOT a boolean comparison operator (used to compare 2 values in a Boolean expression)? a. >= b. != c. = d. > e. They are all boolean comparison operators 9. For what values of temperature will the output to the screen be “Just right!”? if (temperature <= 65 ) { cout << “Too cold!\n”; } else if (temperature <= 85) { cout << “Just right!\n”; } else { cout << “Too hot!\n”; } a. All values less than or equal to 65 b. All values less than or equal to 85 c. All values between 65 and 85, including 65 and 85 d. All values between 65 and 85, including 65 but not 85 e. All values between 65 and 85, including 85 but not 65 f. All values greater than or equal to 85 10. What is the output of the following code fragment? int i = 0; while (i <= 10) { i = i + 2; cout << i << “ “; } Output ______2 4 6 8 10 12_________________ 11. What is the output of the following code fragment? int i = 30; while (i >= 20) { cout << i << “ “; i = i – 2; } Output _______30 28 26 24 22 20___________________ 12. The following loop sums positive integers read in from the user keeping track of how many were read in. Give an expression for the loop condition if we choose to end the loop with a sentinel value of a negative number read in from the user. int next = 0, sum = 0, cnt = -1; cout << “Enter as many positive integers as you want. “; cout << “To stop enter a negative number\n”; do { cnt++; sum += next; cin >> next; }while ( ?? ); Answer _____next>=0___________ 13. What is the return type of the function declared below? int return_grade(char score); a. int b. grade c. char d. score e. none of the above 14. Which of the following function headers is the best choice for this function definition? ?(function header goes here) { cout << “Enter the first value\n”; cin >> first; cout << “Enter the second value\n”; cin >> second; return; } a. void get_input(int first, int second) b. void get_input(int& first, int& second) c. int get_input(int first, int second) d. int get_input(int& first, int& second) 15. Which one of the following statements is true? a. Void functions must contain a return statement. b. Void functions must not contain a return statement. c. Non-void functions must not contain multiple return statements. d. Non-void functions must contain a return statement. e. Non-void functions must not contain a return statement. 16. Give a correct call to the function prototype: int something(double x, int y, char z); int my_var = something(8.5, 9, ‘a’); 17. Which of the following is a correct call to the function declared below? You may assume the variables double_var, int_var, and char_var were declared and initialized as the correct type. void doSomething(double& x, int& y, char& z); a. void doSomething(double& x, int& y, char& z); b. void doSomething(double_var, int_var, char_var); c. doSomething(double_var, int_var, char_var); d. doSomething(double& x, int& y, char& z); e. doSomething(8.5, 9, ‘a’); f. c and e 18. What is the output of the following code fragment given this definition of the function something? int something(int& a, int b) { int c = a + b; a = a * 2; b = c + a; return c; } // a fragment of main int r = 1, s = 2, t = 3; s = something(t, r); cout << r << “ “ << s << “ “ << t << endl; a. 1 4 3 b. 10 4 6 c. 1 4 6 d. 4 3 10 e. 10 4 3 f. 1 2 6 19. Which of the following statements output the value 3.5 to the screen? a. cout << double(7) / 2; b. cout << double(7 / 2); c. cout << 7 / 2; d. cout << 7 / 2.0; e. a, b, and d f. a and d 20. Which of the following Boolean expressions evaluate to true? a. true || true b. true || false c. false || true d. false || false e. a, b, and c 21. What is the output of the following code fragment assuming fin is an ifstream object that has been connected to the file in.txt correctly? Assume in.txt contains this: 7 3 1 7 1 2 4 7

int next, sum = 0; while (fin >> next) { if ((next % 2) = = 0) { sum += next; } } cout << sum; Answer ______6_______ 21. Which statement correctly opens the file input.txt and connects it to the ifstream object fin? a. open(fin, “input.txt”); b. fin = “input.txt”; c. fin = open(“input.txt”); d. connect(fin, “input.txt”); e. fin.open(“input.txt); Use the following structure definitions and object declarations for the next 5 questions (#22 - # 26) struct Book struct Child { { string name; string name; string author; double height; int num_of_pages; double weight; char type; int age; }; Book favorite_book; }; Child next_child, prev_child; Book new_book; 22. Which statement correctly initializes the name of a child’s favorite book? a. next_child.favorite_book.name = “Goodnight Moon”; b. next_child.name = “Goodnight Moon”; c. next_child.favorite_book = “Goodnight Moon”; d. Child.Book.name = “Goodnight Moon”; e. favorite_book.name = “Goodnight Moon”; 23. Which statement correctly initializes a child’s age? a. Child.age = 2; b. next_child = 2; c. next_child.age = 2; d. age = 2; 24. Which of the following is NOT a legal C++ statement? a. prev_child = next_child; b. Book old_book = new_book; c. Child new_child = prev_child.name; d. next_child.age = 3; e. next_child.weight = prev_child.weight; 25. What is the type of next_child.favorite_book.num_of_pages? Answer ____int_________ 26. What is the type of new_book? Answer _______Book_______ 27. What would be the output of the following code fragment? for (int i = 9; i > 0; i = i - 2) { cout << i << “ “; } a. 9 8 7 6 5 4 3 2 1 b. 9 8 7 6 5 4 3 2 1 0 c. 9 7 5 3 1 d. 9 7 5 3 1 -1 e. infinite loop 28. For the arrays declared below, explain which of the following statements should not be used: int score[5] = {1, 2, 3, 4, 5}; char grade[5] = {‘F’, ‘D’, ‘C’, ‘B’, ‘A’}; a. if (stu_score == score[i]) cout << grade[i]; b. cout << score[0]; c. cin >> score[5]; d. while (grade[i] > ‘F’) cin >> grade[i]; Explain why _________overruns array boundary and causes program to crash_____________ 29. Assuming the array below had been initialized to some values, which statement would correctly move each element of the array up one spot. Don’t worry about the one lost value, but do not access values “out of bounds”? int array[10]; a. for (int i = 0; i < 9; i = i + 1) array[i] = array[i + 1]; b. for (int i = 0; i < 10; i = i + 1) array[i] = array[i + 1]; c. for (int i = 1; i < 9; i = i + 1) array[i - 1] = array[i]; d. for (int i = 1; i < 10; i = i + 1) array[i - 1] = array[i]; e. a and d 30. What is the output of the following code? int array[5]; for (int i = 0; i < 5; ++i) { array[i] = i * 3; } for (int i = 4; i >= 0; --i) { cout << array[i] << “ “; } a. 4 3 2 1 b. 4 3 2 1 0 c. 12 9 6 3 d. 12 9 6 3 0 e. 9 6 3 0 31. Why should you use a named constant for the size of an array? a. Readability of code b. Makes changes to the program easier c. Helps reduce logic errors d. All of the above 32. Which is a correct call to this function? int out_of_order(double *array, int size) { for (int i = 0; i < size – 1; i = i + 1) { if (array[i] > array[i + 1]) { return (i +1); } } return -1; } a. cout<< out_of_order(double *array, int size) b. cout << out_of_order(*arr, 5) c. cout << out_of_order(arr, 5) d. cout << out_of_order(double array[size]) 33. Explain why which of the following is NOT a legal statement in C++? a. string name; b. string name = ""; c. string name = “Justin”; d. They are all legal statements. Explain why ______________________________ 34. The syntactically correct way in C++ to declare and initialize, in a single step, a pointer to a previously declared variable s, of type double, is a. double* p = &s; b. double &p = *s; c. double* p = s; d. double p = *s; e. double* p = *s; 35. After the following statements are executed, what can be said about variables x and p? char x = ‘M’; char* p = &x; *p = ‘W’; a. p holds the value ‘M’. b. p holds the value ‘W’. c. p points to the variable x, which has the value ‘M’ d. p points to the variable x, which has the value ‘W’ e. p holds the value ‘W’, x holds the value ‘M’ f. The statements cannot be executed because of a compiler error. 36. If p1 and p2 are pointers to two different variables v1 and v2 of the same type, with p1 pointing to v1 and p2 pointing to v2, then which of these statements is correct? a. *p1 = *p2; makes the value of p1 equal to the value of p2. b. *p1 = *p2; makes the value of v2 equal to the value of v1. c. *p1 = *p2; makes the value of p2 equal to the value of p1. d. *p1 = *p2; makes the value of v1 equal to the value of v2. e. None of the above. 37. Given that int* p; declares a pointer to an integer, which of the following is not correct: a. *p = 7; is a valid C++ statement. b. p = new int[7]; is a valid C++ statement. c. Following the declaration, p points to an integer variable allocated in the heap area. d. p = &x; is a valid C++ statement, provided that x is declared as an integer variable in the same scope of p and declared prior to the statement.

38. Suppose you have a pointer variable p and you execute the statement delete p;. What happens? a. p is deleted from the heap. b. p is deleted from the stack. c. The variable which p points to is deleted but p still remains. d. Only the value of the variable which p points to is deleted; the variable itself remains. e. Both the variable which p points to, as well as p itself, are deleted.

39. What happens when the statement bool *p = new bool(true); is executed?[NOTE -- you may not have seen this syntax before -- make an educated guess, but don't sweat this one] a. p is set to the value true. b. A nameless variable of type bool is allocated in the heap and given the value true. c. A nameless variable of type bool is allocated in the stack and given the value true. d. The statement cannot be executed because it does not compile under any standard implementation of the C++ language. e. The argument true is passed to the function named bool and the result is assigned to *p.

40. Suppose that p1 and p2 are both pointers to the same integer variable x; y is another integer variable. After the execution of which of the statements below is the pointer variable p1 considered a dangling pointer? a. x = 0; b. p1 = &y; c. p2 = new int(7); d. delete x; e. delete p2;

41. After the following statements are executed, what is the value of *p? char x = ‘M’; char * p = NULL; p == &x; p = ‘W’; a. ‘M’. b. ‘W’. c. The statements cannot be executed because the compiler complains about the statement p == &x;. d. The statements cannot be executed because the compiler complains about the statement p = ‘W’;. e. None of the above.

42. All the code between #ifndef MYSTRUCT_H and #endif

is ____________ if MYSTRUCT_H is defined. a. skipped b. executed c. compiled d. debugged

43. What (if anything) is wrong with the following recursive function? It should print out the contents of the array backwards. void print(int* array, int start, int size) { if(start == size) return; else { print(array, start+1,size); cout << array[start] << endl; } }

a. infinite recursion b. the stopping condition is wrong c. the recursive call is wrong d. nothing, this works fine as defined 44. Given the below struct Node, and assuming that LIST is a Node* that points to the beginning of a valid linked list shown below, give C++ source code that sums the data fields of the list and prints the answer to the console. Be sure that you do not lose the head of the list! struct Node { int data; Node* next; };

Node* ptr = LIST; // don’t want to lose the head of the list int sum = 0 while (ptr != 0) { sum += ptr->data; ptr = ptr->next; } cout << “Sum of the data fields of LIST is: “ << sum; 45. Fibonacci numbers are a fascinating number sequence that crops up in the most unexpected places – from galaxies to snails to the stock market (show me the money!). The sequence is defined as: F( 0 ) = 0; F( 1 ) = 1; (termination) F( n ) = F( n-1 ) + F( n-2 ) so the first few numbers in the sequence are: F(0) = 0, F(1)=1, F(2) = 1, F(3) = 2, F(4) = 3, F(5) = 5, F(6) = 8, F(7) = 13, F(8) = 21, F(9) = 34 …. Define a recursive function to calculate the nth Fibonacci number. The function prototype is: int fib( int n ); //returns F(n)

int fib( int n ) //1 point { if ( n == 0 ) //2 points return 0; if ( n == 1 ) //2 points return 1; return ( fib( n – 1 ) + fib (n – 2 ) ); //5 points } 46. Now define an iterative version of the fibonacci function. Use the same function prototype as above. Remember that each term is built up from the values of the two terms before it, meaning that inside the loop body you will need to hold the values of two previous terms. (It might ((or might not)) help you to think of the “swap” function).

int fib( int n ) //1 point { if ( n == 0 ) //2 points return 0; if ( n == 1 ) return 1; //2 point int fib_n; int fib_n_minus_2 = 0; //2 points int fib_n_minus_1 = 1; //2 points for ( int i = 0; i < (n – 1); ++i ) //2 points { fib_n = fib_n_minus_1 + fib_n_minus_2; //3 point fib_n_minus_2 = fib_n_minus_1; //1 point fib_n_minus_1 = fib_n; //1 point } return fib_n; //1 point }

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.