UNIVERSITY OF LONDON GOLDSMITHS COLLEGE Department of Computing B. Sc. Examination 2018 IS52028A Principles and Applications of Programming Duration: 2 hours 15 mins Date and time: TThis paper is in...

This is going to be an exam on java and it will start tonight form midnight and will finish friday at 12 noon


UNIVERSITY OF LONDON GOLDSMITHS COLLEGE Department of Computing B. Sc. Examination 2018 IS52028A Principles and Applications of Programming Duration: 2 hours 15 mins Date and time: TThis paper is in three parts: part A (Multiple Choice), part B (Java) and part C (C++). You should answer one question from part A (either Java or C++) and either TWO questions from part B (Java) or TWO questions from part C (C++). Part A carries 40 marks, and each question from parts B and C carries 30 marks. The marks for each part of a question are indicated at the end of the part in [.] brackets. There are 100 marks available on this paper. Electronic calculators must not be programmed prior to the examination. Calculators which display graphics, text or algebraic equations are not allowed. THIS PAPER MUST NOT BE REMOVED FROM THE EXAMINATION ROOM IS52028A 2018 page 1 of 19 TURN OVER Part A Multiple choice IS52028A 2018 page 2 of 19 Question 1 Multiple choice JAVA (a) Considering the following code, which of the following are true? Select all that are true. You will score one point for each correct answer and lose one point for each wrong answer. 1 2 class Car { 3 private int speed; 4 5 Car (){ 6 speed = 0; 7 } 8 Car (double n){ 9 new Car(); 10 } 11 public static void main(){ 12 Car car = new Car (10); 13 } 14 } [2] i. speed is initialised to zero in the constructor. ii. The code defines a new variable type called var. iii. speed is initialised to 10 in the constructor. iv. the variable speed can be directly accessed from any sub-class of the Car class. IS52028A 2018 page 3 of 19 TURN OVER (b) Which is the correct value and explanation for the value of the colour variable inside the ford object at the end of main? [2] 1 2 class Car{ 3 private String colour; 4 5 Car(String _colour){ 6 colour = _colour ;; 7 } 8 9 void setColour(String _colour){ 10 colour = _colour; 11 } 12 13 static void respray(Car c){ 14 c.setColour("green"); 15 } 16 17 public static void main(String args []){ 18 Car ford = new Car("blue"); 19 Car Subaru = new Car("orange"); 20 respray(ford); 21 System.out.println(ford.colour); 22 } 23 24 } [ i. It is blue because the respray function implements pass by reference, not pass by value. ii. It is blue because the respray function implements pass by value, not pass by reference. iii. It is green because the respray function operates upon the ford object, setting its class member colour to a new value. iv. It is not possible to know what the colour would be as the ford variable is not returned from the respray function. IS52028A 2018 page 4 of 19 (c) Which one the following is a valid call to the constructor of class Car? ( [2] i. Car car; ii. Car car = new Car(); iii. Car new car; iv. none of the above (d) which one statement is correct about the following code snippet? [2] int i ; i = "2"; System.out.println(i); i. it prints the string ”2” ii. it prints the integer 2 iii. compilation error iv. runtime error (e) What is the value of x at the end of main and why? [2] void funct(){ int x = 10; float y = 10.5f ; x ++; x = x + y; } i. 22 because y is rounded up when it is added to x. ii. 21 because y is rounded down when it is added to x. iii. This code will not compile as it assigns a float value to an int. iv. 20 because y is rounded down and when it is added to x. IS52028A 2018 page 5 of 19 TURN OVER (f) What is the difference between an ArrayList data structure and an array data structure? [2] i. An ArrayList is a fixed sized data structure whereas an array is a variable sized data structure. ii. An array is a fixed sized data structure whereas an ArrayList can change size. iii. Memory cannot be allocated for arrays at run time. iv. Arrays are not built into the language but ArrayLists are. (g) Which of the following are suitable control flow structures for iteration? Select all that apply. [2] i. for loop. ii. while loop. iii. if statement. iv. switch statement. (h) Which one of the following keywords is used to access member of class before object of that class is created? [2] i. public ii. private iii. static iv. protected (i) Which are true about the following function prototype? Select all that apply. [2] void myGreatFunction(int x, int y); i. It does not return anything. ii. It adds x and y together. iii. It uses pass by reference to access the variables. iv. It takes two parameters, both of which are int type. IS52028A 2018 page 6 of 19 (j) What is wrong with the following code? Select all that apply [2] import java.util.*; public class Test{ public static void main(String args[]){ Map facts = new HashMap<>(); facts[10] = "275 million stars are born every day."; } } i. map is not a correct type. ii. facts maps from string to string so the 10 in facts[10] is not a valid key type. iii. The string contains the number 275 so it is not a valid string. iv. There should not be a comma between String and String. (k) In order for the following code to be correct, what must be the type of the reference variable card? [2] 1 card.greeting (); 2 3 card = new Holiday( "Bob" ) ; 4 card.greeting (); 5 6 card = new Birthday( "Emily", 12 ) ; 7 card.greeting (); i. Valentine ii. Holiday iii. Birthday iv. A type that is the parent class of Valentine, Holiday and Birthday. (l) What is polymorphism in Java? [2] i. It is when a single variable is used with several different types of related objects at different places in a program ii. It is when a program uses several different types of objects, each with its own variable iii. It is when a single parent class has many child classes iv. It is when a class has several methods with the same name but different pa- rameter types IS52028A 2018 page 7 of 19 TURN OVER (m) Which of the following is/are false statements? [2] i. final method can be inherited ii. final class cannot be inherited iii. final variable of a class cannot be changed iv. final method can be overridden (n) The order of execution of constructors in Java Inheritance is: class Vehicle { Vehicle(){} } class Car extends Vehicle{ Car(){} public static void main(String [] args) { Car car = new Car(); } } [2] i. Car then Vehicle ii. Car only iii. Vehicle then Car iv. Vehicle only IS52028A 2018 page 8 of 19 (o) What is the output of the program below: [2] 1 static void print(int x, int y){ 2 System.out.println("Hi"); 3 } 4 5 static void print(int a, int b){ 6 System.out.println("Hello"); 7 } 8 9 public static void main(String [] args){ 10 print (1,2); 11 } i. it has a compilation error ii. it has a run time error iii. it compiles, runs and prints the string ‘Hello’ iv. it compiles, runs and prints the string ‘Hi’ (p) Given the following method: 1 public static void mystery(int n) { 2 if(n==0){ 3 return; 4 } 5 if(n%3 == 0){ 6 System.out.println(n); 7 } 8 mystery(n-1); 9 } What is printed when mystery(5); is executed? [2] i. 0 ii. 3 iii. 5 iv. 4 IS52028A 2018 page 9 of 19 TURN OVER (q) What is the output of the following program? [2] 1 class C1{ 2 public C1(){ 3 System.out.print (1); 4 } 5 } 6 class C2 extends C1{ 7 public C2(){ 8 System.out.print (2); 9 } 10 } 11 class C3 extends C2{ 12 public C3(){ 13 System.out.println (3); 14 } 15 } 16 public class Test{ 17 public static void main(String [] a){ 18 new C3(); 19 } 20 } i. 12 ii. 23 iii. 123 iv. 321 IS52028A 2018 page 10 of 19 (r) What is the output of the following program? [2] 1 class Base{ 2 int value = 0; 3 Base(){ 4 addValue (); 5 } 6 void addValue (){ 7 value += 10; 8 } 9 int getValue (){ 10 return value; 11 } 12 } 13 class Derived extends Base{ 14 Derived (){ 15 addValue (); 16 } 17 void addValue (){ 18 value += 20; 19 } 20 } 21 public class Test{ 22 public static void main(String [] args){ 23 Base b = new Derived (); 24 System.out.println(b.getValue ()); 25 } 26 } i. 10 ii. 20 iii. 30 iv. 40 IS52028A 2018 page 11 of 19 TURN OVER (s) What is the output of this program? [2] 1 class A 2 { 3 public int i; 4 private int j; 5 } 6 class B extends A 7 { 8 void display () 9 { 10 super.j = super.i + 1; 11 System.out.println(super.i + " " + super.j); 12 } 13 } 14 class inheritance 15 { 16 public static void main(String args []) 17 { 18 B obj = new B(); 19 obj.i=1; 20 obj.j=2; 21 obj.display (); 22 } 23 } i. 22 ii. 33 iii. run time error iv. compilation error IS52028A 2018 page 12 of 19 (t) What will be the result of compiling and running the following code? [2] 1 class A{ 2 int b=10; 3 private A(){ 4 this.b=7; 5 } 6 int f(){ 7 return b; 8 } 9 } 10 class B extends A{ 11 int b; 12 } 13 public class Test{ 14 public static void main(String [] args){ 15 A a = new B(); 16 System.out.println(a.f()); 17 } 18 } i. compilation error ii. prints 0 iii. prints 7 iv. prints 10 IS52028A 2018 page 13 of 19 TURN OVER Part B Written answer questions JAVA IS52028A 2018 page 14 of 19 Question 2 Data Structure – JAVA (a) What is the output of the following program: 1 2 public class MyClass 3 { 4 public static void main(String [] args) { 5 Integer [] myArray = {3, 1, 2}; 6 List list = Arrays.asList(myArray); 7 Set set1 = new HashSet (); 8 TreeSet set2 = new TreeSet (list); 9 set1.addAll(set2); 10 set1.add (4); 11 set2.remove (5); 12 String result = ""; 13 Iterator myIterator = set2.iterator (); 14 while (myIterator.hasNext ()) 15 result += myIterator.next(); 16 System.out.println("result: " + result); 17 } 18 } [6] IS52028A 2018 page 15
Jun 02, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here