COS1511 Supp 2024 exam paper — questions

🔒 Scan locked

The paper, as written

Every question, by topic

  1. Question 1.1 · Selection: if/else and switch statements · 2 marks

    A C++ program declares three integers: var1 is initialised to 4, var2 to 10, and newval to 0. The program then checks: if var1 multiplied by 2 is greater than or equal to var2, newval is set to 5 plus 2 times var2; otherwise, if var1 is less than var2, newval is set to var2 minus var1 times 2; otherwise (if neither condition holds), newval is set equal to var1. Determine what value newval will hold once this code has finished executing.Show the full question
  2. Question 1.2 · Selection: if/else and switch statements · 2 marks

    An integer variable a is declared and its value is read in from input as 5. The program then executes a switch statement on a with the following cases: case 1 adds 3 to a; case 3 multiplies a by 3 and then breaks; case 5 pre-increments a and adds 10 to it; case 6 divides a by 2; and the default case adds 1 to a. Note that only case 3 contains a break statement, so execution falls through from one matching case to the next until a break is reached or the switch ends. Determine the final value of a after this switch statement has been executed with the input value 5.Show the full question
  3. Question 1.3 · Selection: if/else and switch statements · 2 marks

    A function named findValue takes one integer parameter, numberP. Inside the function, an integer count is initialised to 0 and an integer value is initialised to 20. A while loop runs as long as count is less than numberP; on each iteration, value is increased by the current value of count, and then count is incremented by 1. Once the loop ends, the function returns value. Given the statement cout << findValue(3); executed in the main function, determine what output this statement will produce.Show the full question
  4. Question 2.1 · Code tracing and variable diagrams · 2 marks

    Before answering the two parts below, study this worked example of what is meant by describing a code segment's purpose: given the declarations int a,b,c; followed by cin >> a >> b >> c; and then cout << c + b + a;, the purpose of this segment is to input three integer values and display their sum. Using this same style of explanation, describe in words what each of the following C++ code segments achieves. Assuming that the integer variables s and n have already been declared, explain in words what the purpose of the following code segment is: s is initialised to 0 and n is initialised to 0; then, while n is less than or equal to 5, the loop body adds n to s (s = s + n) and increments n by 1 (n++).Show the full question
  5. Question 2.2 · Code tracing and variable diagrams · 2 marks

    Before answering the two parts below, study this worked example of what is meant by describing a code segment's purpose: given the declarations int a,b,c; followed by cin >> a >> b >> c; and then cout << c + b + a;, the purpose of this segment is to input three integer values and display their sum. Using this same style of explanation, describe in words what each of the following C++ code segments achieves. Explain in words what the purpose of the following code segment is: an integer array called numbers is declared and initialised with the values 11, 0, 15, 0, 16, 23; an integer counter c is initialised to 0; then, for each index i from 0 to 5, if the element numbers[i] is not equal to 0, the counter c is incremented by 1.Show the full question

The full Spot Map and the marks by year — and this paper’s scan.