COS1511 Oct/Nov 2022 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

    Examine this C++ code segment: int var1 = 4; int var2 = 10; int newval = 0; if (var1 * 2 >= var2) newval = 5 + 2 * var2; else if (var1 < var2) newval = var2 - var1 * 2; else newval = var1; After this code executes, what value will newval have?Show the full question
  2. Question 1.2 · Selection: if/else and switch statements · 2 marks

    Given this C++ code segment: int a; cin >> a; switch (a) { case 1: a += 3; case 3: a = a * 3; break; case 5: a = ++a + 10; case 6: a /= 2; default: a = a + 1; } Assuming the input value entered for a is 5, what will the value of a be after this code has been executed?Show the full question
  3. Question 1.3 · Selection: if/else and switch statements · 2 marks

    Consider the following C++ code segment: int findValue(int numberP) { int count = 0; int value = 20; while (count < numberP) { value += count; count ++; } return value; } If the statement cout << findValue(3); is executed in the main function, what will be the output?Show the full question
  4. Question 2.1 · Code tracing and variable diagrams · 2 marks

    An example is given to illustrate how to answer: for a segment declaring int a,b,c; reading in a, b and c with cin >> a >> b >> c; and then outputting cout << c + b + a;, the purpose of the code is described as being to input three integer values and display their sum. Using this style, describe in words the purpose of each of the following code segments. Assuming that s and n have both been declared as integers, consider the segment: int s = 0; int n = 0; while (n <= 5) { s = s + n; n++; }. Explain in words what the purpose of this segment of code is.Show the full question
  5. Question 2.2 · Code tracing and variable diagrams · 2 marks

    An example is given to illustrate how to answer: for a segment declaring int a,b,c; reading in a, b and c with cin >> a >> b >> c; and then outputting cout << c + b + a;, the purpose of the code is described as being to input three integer values and display their sum. Using this style, describe in words the purpose of each of the following code segments. Consider the segment: int numbers[ ] = {11, 0, 15, 0, 16, 23}; int c = 0; for (int i = 0; i <= 5; i++) if (numbers[i] != 0) c += 1;. Explain the purpose of this segment of code.Show the full question

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