First page of the May/Jun 2016 COS1511 paperSee a real past paper — freeMay/Jun 2016 · 75 marks · the scan, as written

How often Selection: if/else and switch statements is asked

8 of 8

papers asked it
avg 20 marks · last Supp 2025

Worth 1–11 marks when it appears as a written question, plus 9 multiple-choice items.

Where it was asked

The questions

  1. May/Jun 2018, Q1.82 marks · multiple choice

    Consider the following code fragment, where x, y and z are int variables: if (x == 2) if (y >= 110) z = y / x; else z = y * x; else z = y + x; Which of the following pieces of code will give exactly the same result as the code above?

  2. May/Jun 2018, Q45 marks

    At a tollgate, the number of vehicles passing through with two wheels, four wheels, and more than four wheels respectively is counted. Write down ONLY the switch statement needed to count the number of vehicles having two wheels, four wheels and more than four wheels; do NOT write a complete program. Use the variable int nrWheels, which stores the number of wheels of a vehicle passing the tollgate, and the variables int count2, count4, countMore, which serve as the counters for the number of vehicles with two wheels, four wheels and more than four wheels respectively. Assume that count2, count4 and countMore have already been initialised, and that a value for nrWheels has already been input and validated.

  3. May/Jun 2018, Q66 marks

    A bank sets its deposit interest rate according to how long funds remain on deposit, using this schedule: a time on deposit greater than or equal to 5 years earns an interest rate of 0.0475; less than 5 years but greater than or equal to 4 years earns 0.045; less than 4 years but greater than or equal to 3 years earns 0.040; less than 3 years but greater than or equal to 2 years earns 0.035; less than 2 years but greater than or equal to 1 year earns 0.030; and less than 1 year earns 0.025. Using this schedule, write a program that accepts the length of time funds have been left on deposit and then displays the interest rate that corresponds to that time, making use of a nested if-statement. Write down ONLY the necessary statements and do NOT write a complete program. Use the following variables: double time, which stores the time the funds are left on deposit, and double interest, which stores the interest rate that corresponds to the time entered.

  4. Oct/Nov 2016, Q3.12 marks

    Rewrite the following switch statement as an equivalent if-else statement: the code switches on the variable level, where case 1 falls through to case 2, and in that combined case the program outputs "Basics" followed by a break; case 3 outputs "Intermediate" followed by a break; case 4 outputs "Advanced" followed by a break; and the default case outputs "Not a valid level".

  5. Oct/Nov 2016, Q3.24 marks

    A program correctly outputs all the prime numbers below 10 using a nested loop. The program includes iostream, uses the std namespace, and in main declares integers i (initialised to 2) and j, and a boolean prime initialised to true. The outer for loop runs with i starting at 2 and continuing while i < 10, incrementing i each time; inside it, prime is set to true, then an inner for loop runs with j starting at 2 and continuing while j < i, incrementing j each time, and inside this inner loop, if i % j equals 0 then prime is set to false. After the inner loop, if prime is true, the program outputs i followed by endl. Rewrite the complete program by replacing both for loops with while loops, keeping the program's behaviour identical.

The full Spot Map and the marks by year.