cos1511may2014 (1) - This paper consists of 8 pages and 10...

This preview shows page 1 - 4 out of 8 pages.

[TURN OVER]This paper consists of 8 pages and 10 questions.Please ensure that you have 8 pages and 10 questions.INSTRUCTIONS:Answer all the questions.Do all rough work in the answer book.The mark for each question is given in brackets next to the question.Please answer the questions in the correct order.If you want to do a question later, leaveenough space.Number your answers and label your rough work clearly.Marks are awarded for part of an answer, so do whatever you are able to in each question.ALL THE BEST!
COS1511May /June 2014[TURN OVER]2QUESTION 18 marksGive the value ofxafter execution of each of the following code fragments:1.1int x = 5;(2)x -= --x;1.2int x = 2 * 3 + 4 * 5 / 4 / 2;(2)1.3int x = 0;(2)for (int i = 0; i < 4; i++){x += (i % 2 ? 1 : 0);}1.4int j = 2;(2)int k = 3;int m = 2;int x = (k = m < j || 3 - j > k);QUESTION 28 marks2.1Consider the following C++ code segment.What value willchave after the code has beenexecuted?(2)int a = 4, b = 2;int c;if (a * b > b * 4)c = 2 * b;else if (b < a)c = b + a * 2;elsec = a;2.2Suppose the input value fortypeis the character‘/’. What is the value ofvalueafter thefollowing C++ code has been executed?(2)int value = 5;char type;cin >> type;switch (type){case '+': value += 5;case '/': value = 100/value;case '-': value = value/2;case '%': value += value % 2 + 4;default: value += value / 3 + 10;}2.3Consider the following C++ code segment below.(2)int result(int valueP){int count = 0;int a = 2;
COS1511May /June 2014[TURN OVER]3while (count < valueP){a += count + a / 2;count += 2;}return a;}What will the output of the program be if the following instruction appears in themainfunction?cout << result(6);2. 4The video shop has a special discount under the following conditions:(2)It is Tuesday or Thursday , ANDYou hire 3 old DVDs.Astringvariabledayrepresents the day of the week. Theintvariableoldrepresent thenumber of old DVDs you want to hire. Theboolvariablediscountshould receive the valuetrueif the above conditions are met. Write the statement that will assign the correct value todiscount?QUESTION 39 marks3.1Consider the followingnested ifcode fragment. Note thaticecreamis astringvariableandchoiceis anintvariable. Convert the code by making use of theswitchstatement.(4)if (choice == 1)icecream = “vanilla”;else if (choice == 3)icecream = “chocolate”;else if (choice == 2)icecream = “blueberry”;else icecream = “rum & raisin”;3.2Convert the followingforloop into awhileloop.for (int i = 1; i <= n; i++)cout << i * i;3.3The following code is supposed to write out the positive even numbers less than 12.Thatis, it will output the numbers2,4,6,8,and10. However, there is a problem.Explain theproblem and correct the code.(2)int x = 1;while (x != 12){cout << x << endl;x = x + 2;}(3)

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 8 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Summer
Professor
N/A
Tags
Past Exam Paper, ASCII, include, addAndMultiply

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture