name: first ________________ last _____________________ CS 13001 "Computer Science I" EXAM #1 The number of points for each question is given in square brackets. For all code fragments below assume that appropriate #include and "using" directives were provided. 1.[10] Consider the following code fragment: string s1 = "Hi", s2 = "Hello", s3; s3 = (s1 < s2) ? s1 : s2; cout << s3; Write out what output this code generates. Name the operation on the second line of the fragment and explain how it operates. Rewrite the second line using either branching or looping statements. 2.[10] Explain the differences between prefix and suffix form of the increment operator. Consider the following code fragment: int a=3, b=4, c; c = (a++) - (++b); cout << a << " " << b << " " << c; What output does it generate? Explain your answer. 3.[10] Explain the difference between while and do-while looping constructs. Rewrite the following code with either while or do-while. for(int i=0; i < 10; i++) cout << i << ' '; 4.[15] Describe how "break" and "continue" statements work with looping constructs. Consider the following code fragment: int i=0; while (i < 10){ i++; if (i > 5) break; cout << i << " "; } cout << i; What out would this code generate? How would the output change if "break" is replaced by "continue"? Explain your answers. For both cases write out the exact output. 5.[10] Define the notion of variable scope. Consider the following code fragment: int a = 3; if (a > 0) { double a = 10.0; cout << a << " "; } cout << a; Write out what output this code generates. Explain your answer. 6.[15] Name and explain parameter-passing disciplines we studied. Consider the following code fragment: int function1(int); int function2(int&); int main(){ int a=3, b=4; int c = function1(a); int d = function2(b); cout << a << " " << b << " " << c << " " << d; } int function1(int aa){ aa++; return aa; } int function2(int &bb){ bb++; return bb; } Write out what output this code generates. Explain your answer. 7.[15] Consider the following code fragment: const int SIZE=10; int myarray[SIZE]; // here goes the code that assigns the array // values to each element of the array // assume it already exists do not write it int number; cout << "Input the number to search for: "; cin >> number; // // here goes the code you need to write // Complete this fragment such that the resultant code checks whether the input number is in the array "myarray" and outputs the result. Specifically, your code needs to produce one of the two outputs example one: Input the number to search for: 60 FOUND example two Input the number to search for: 33 NOT FOUND That is, depending on the input your code needs to output either "FOUND" or "NOT FOUND". Write out the code. Explain how it works. 8.[15] Describe what structures in C++ are and what their purpose is. Explain what structure variable and member variable are. Assuming the following structure definition. struct exampleStruct { int a; string b; }; consider the below code: exampleStruct x[10]; Write code that assigns a value to every member variable of the fifth element of the array "x". The value is of your choosing. Explain the operation of your code.