Problem Solving Array: 1. The array x contains the following values: 23 12 13 17 23 19 What is the value of m after the following code is executed? int x[6]={23,12,13,17,23,19}; int m=0; for(int i=0;i<6;i++) if(x[i]>x[m]) m=i; a) 0 b) 1 c) 4 d) 5 2. The array data contains 6 elements. The code below is intended to shift the elements in data to the left with wrap around. EX: That is to convert 7 3 8 1 0 5 into 381057 int data[6]={7,3,8,1,0,5}; ------------------ // statement 1 for(int i=0;i<=4;i++) { data[i]=data[i+1]; ------------------ // statement 2 } In order for the code to execute correctly “statement 1” and “statement 2” should be a) int temp = data[0]; data[0] = temp; b) int temp = data[0]; data[4] = temp; c) int temp = data[5]; data[0] = temp; d) int temp = data[0] data[5] = temp; 3. What will be the values of elements of array a after executing the following code? int a[10] = {10, 5, 8, 2, 4, 9, 3, 20, 15, 18}; for (int i = 0; i < 9; i++) { int j = i + 1; while (j < 10) { if (a[i] > a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } j++; }} 1 a) b) c) d) 10 18 2 20 5 15 3 18 8 20 4 15 2 3 5 10 4 9 8 9 9 4 9 8 3 2 10 5 20 8 15 4 15 5 18 3 18 10 20 2 Pointer: 4. Consider the following code? int x=5, *ptr, &y = x; ptr = &x; Which statement of the following can be written to increment the variable x? a) ++x; b) ++y; c) ++*ptr; d) A or B or C 5. What does the following code print on the output screen? int x = 10; int *ptr; ptr = &x; *ptr +=10; cout << & ptr << “ “ << *ptr; a) b) c) d) The address of x, the content of x The address of ptr, the content of x The address of ptr, the content of ptr The content of x, the content of ptr String: 6. What does strcat (A, "This"); do? a) Copies "This" into A c) Compares A and "This" b) Adds "This" to the end of A d) Copies the first 4 characters of A with "This" 7. What character terminates all character array strings? a) \0 b) . c) \END d) \t Function: 8. Consider the following code. int f () { int k, result; result = 0; for ( k = 0; k < 5; k++ ) { if ( ( k % 3 ) == 1 ) result = result + k; else result = result + 1; } return result; } 2 What value is returned as a result of the call f () ? (A) 5 (B) 6 (C) 7 (D) 8 (E) 9 9. Consider the following code. int fun (int &x, int &y, int &z) { if (x > 6) y = 25; z = x + y; return y + z; } int main() { int a,b,c,i; a=6; b=7; c=8; i = fun(a, b, c); cout<< i<< a<< b<< c; return 0; } If fun uses call-by-reference, what values would be printed as a result of executing the pseudocode? i a b c (A) 6 20 8 7 (B) 6 20 13 7 (C) 20 6 7 8 (D) 20 6 7 13 (E) 56 6 25 31 10. Consider the following pseudocode program. int i; void S () { cout<< i ; cout<< " "; } void R () { int i; i = 2; cout<< i ; cout<< " "; S (); } void main () { i = 3; S (); R (); } 3 What is the output of the program if the pseudocode uses either static (lexical) scoping or dynamic scoping? Static Scoping Dynamic Scoping (A) 3 2 3 (B) 3 3 2 (C) 3 3 2 (D) 3 3 3 (E) 3 3 3 11. Consider the following C++ Program: int fun (int & y) { y += 5; return 4; } void main ( ) { int x = 3; x = x + fun (x); … } What is the value of x after executing the second assignment statement in main, assuming Operands are evaluated right to left. a) 4 b) 12 c) 7 d) 9 12. What will be the values of elements of array b after executing the following code? void array (int a[], int size) { for (int i = 0; i < size-1; i++) a[i]++; a[size-1] += 15; } void main() { const int size = 4; int b[size] = {3, 2,1, 5}; array(b, size); } a) b) c) d) 3 2 1 4 2 3 2 3 1 1 3 2 5 5 5 20 13. The following function sums the integers from low through limit, inclusive: int Sum( int low, int limit ) { if ( ) // Condition is missing return limit; else return low + Sum (low + 1, limit); } What is the missing If condition? 4 a) low == 1 c) low == limit b) limit == 1 d) low > limit Questions 14-15 are based on the following function. int a,b; int enigma(int &x, int &y) { y = y + b; x = b + x; b = x + b; a = y; } int main() { a = 2; b = 7; enigma(a, b); cout<<a<<”\t”<<b; return 0; } The output of procedure mystery depends on the parameter-passing method used. 14. Suppose that all parameters are passed by value. Which of the following values are output when procedure mystery is called? a) 2 7 b) 2 9 c) 9 14 d) 14 16 15. Suppose that all parameters are passed by reference. Which of the following values are output when procedure mystery is called? a) 2 7 b) 9 14 c) 14 16 d) 30 30 16. Consider the following program then choose the correct option. bool Z(string pString) { int i, j; bool response; i = 0; j = pString.length()-1; response = true; 5 while (i < j) { if (pString [i] != pString[j]) response = false; i=i+1; j=j-1; } return response; } int main() { cout<<Z("noon"); cout<<Z("madam"); return 0; } a) After the code is executed noon is True and madam is False. b) After the code is executed noon and madam have undefined values c) After the code is executed noon and madam are both True. d) After the code is executed noon and madam are both False. 17. Suppose we have this method: void f (int b[ ] ) { b[0]++; } What is printed by these statements? int x[100]; x[0] = 2; f (x); cout<<(x[0]); a) 0 b) 1 c) 2 d) 3 18. Given the following function int next(int x) { return (x+1); } What is the output of the following statement? cout<<next(next(5)); a) 5 6 b) 7 c) 6 d) 8 Recursive function: 19. Given the recursive function int Sum( int n ) { if (n < 8) return n + Sum(n); else return 2; } what is the value of the expression Sum(5) ? A) 5 B)13 C) 20 D) 28 E)none--the result is infinite recursion 20.The factorial of an integer is the product of that integer multiplied by all the positive non-zero integers less than that integer. So, 5! (! is the mathematical symbol for factorial) is 5 * 4 * 3 * 2 * 1. 4! is 4 * 3 * 2 * 1, so 5! could be written as 5 * 4!. So a recursive definition of factorial is n! is n * (n-1)!, as long as n >1. 1! is 1. What is the recursive call for this function (fact)? a) fact(n) * n; b) fact(n-1) * n; c) (n-1) * fact(n) b) fact(n-2) * (n-1) 21. What is the output of sum(4); sum(n) { if (n==1) return 2; else return 2+sum(n-1); } a) 8 b) 6 c) 10 d) 14 Sum(4) 2+sum(3)= 2+sum(2) 2+sum(1)=4 7 22. What is the output of the following code fragment? int f1(int n, int m) { if(n < m) return 0; else if(n == m) return m + f1(n-1, m); else return n + f1(n-2, m-1); } int main() { cout << f1(5,4); return 0; } a) 0 b) 2 c) 4 d)8 23. What is the output of the following code? void mystery(int number) { if ( number > 0) { mystery(number / 2); cout<< number % 2; } else cout<<"0"; } int main() { mystery(7) ; return 0; } a) 0111 b) 1011 c) 1110 d) 1011 Struct: 24. A struct is a ------------ data type. a) Simple b) Structured c) Homogeneous 8 } d) Linked 25. Consider the following code segment: struct Employee { int Eno; float Salary; }; Which of the following variable declarations is correct? a) Emp1 Employee; b) struct Employee( ); c) Employee Emp1; d) Employee Emp1( ); 26. Consider the following code segment: struct Employee { int Eno; float Salary; }; Employee Emp1; By which statements you can assign the values 101 and 500 to Eno and Salary respectively for employee Emp1? a) b) c) d) Emp1Eno = 101; Eno.Emp1 = 101; Emp1.Eno = 500; Emp1.Eno = 101; Emp1Salary = 500; Salary.Emp1 = 500; Emp1.Salary = 101; Emp1.Salary = 500; ` File: 27. What is written to the file MyFile.dat after executing the following code: ofstream obj_out("My_File.dat", ios::out); int x = 10; obj_out << x++; obj_out << ++x; A) 1011 B) 1112 C) 1012 D) 1111 28. Given the above file (2nd_Exam.dat) what is the output of the following code: 9 void main(){ ifstream to_read("2nd_Exam.dat", ios::in); int x; to_read >> x if(x%2==0) cout << x * 10 << endl; else cout << x * 20 << endl; } a) b) c) d) 10 80 40 4 0