Presentation is loading. Please wait.

Presentation is loading. Please wait.

Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;

Similar presentations


Presentation on theme: "Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;"— Presentation transcript:

1 Last Revision

2 Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length; i++) a[i].x = i; What is the problem with this code? Fix it.

3 Answer 1 The problem is that although the array object was created, none of the elements of the array have been initialized to refer to objects of class C. To x it, the loop should be: for(int i = 0; i < a.length; i++) { a[i] = new C(); a[i].x = i; }

4 Question2 Other novice Java programmers might write, class C { int f() { return System.in.read(); } and get a compiler error that says something about System.in.read() throwing an exception. What is the problem? Fix it.

5 Answer 2 Since System.in.read() might throw an exception, either a try block is required to catch the exception or f() has to be declared as throwing an exception. Thus, either the body of f() should be try { return System.in.read(); } catch (Exception e) { return 0; } //some int value must be returned or the declaration of f() should be int f() throws Exception {

6 Question 3 In Java, write a single class called Ouch which, when an Ouch object is created, draws a frame on the screen that contains a button. When the button is clicked upon, \ouch" is printed on the standard output (i.e. using System.out.println). Be sure not to define any other class (either named or anonymous).

7 Answer 3 class Ouch extends Frame implements ActionListener { Ouch() { super("Ouch Frame"); setSize(200,200); Button b = new Button("ouch"); b.addActionListener(this); add(b); setVisible(true); } public void actionPerformed(ActionEvent e) { System.out.println("Ouch"); }

8 Question 4 The Java API defines the Comparable interface as interface Comparable { int compareTo(Object); } where x.compareTo(y) should return -1 if x is less than y, 0 if they are equal, and 1 otherwise. Define a class MyArray that implements Comparable and whose objects behave like integer arrays. MyArrays should be compared based on the sum of their elements. For example: int[] a = new int[] {1,2,3,4}; //create an array and initialize the elements int[] b = new int[] {-1,2,-3,4,-5}; MyArray m1 = new MyArray(a); //the elements of m1 are those of a MyArray m2 = new MyArray(b); //the elements of m1 are those of b System.out.println(m1.compareTo(m2)); //prints 1, since 1+2+3+4 > -1+2-3+4-5 Define just enough of the MyArray class for the above code to work.

9 Answer 4 class MyArray implements Comparable { MyArray(int[] b) { a = b; } public int compareTo(Object o) { MyArray other = (MyArray) o; int mysum = 0; int othersum = 0; for(int i=0; i<a.length;i++) mysum = mysum + a[i]; for(int i=0; i<other.a.length;i++) othersum = othersum + other.a[i]; if (mysum < othersum) return -1; else if (mysum == othersum) return 0; else return 1; } private int[] a; }

10 Question 5 Consider the following fragment of code: int temp, a = 1, b = 2; temp = a; b = a; a = temp; Which one of the following gives the values contained by the variables ’a’ and ’b’ after the above code is run? A. a = 1 and b = 1 B. a = 1 and b = 2 C. a = 2 and b = 1 D. a = 2 and b = 2 E. a = 0 and b = 2

11 Answer 5 The answer is A

12 Question 6 Where the error: public class twice { public static void main( String [] args ) { int n = 1; while (n < 10000) { System.out.print(n + " "); doubleIt(); } System.out.println(); } public static void doubleIt() { n = n * 2;} }

13 Question 6 (cont) The program fails even to compile. Which one of the following reasons causes this failure to compile? A. the class name must have a capital letter ’T’ B. the variable ‘n’ is used in the doubleIt() method but is not available for use in that method. C. the integer variable has not had memory allocated for it D. a class cannot have all its methods declared as static E. the while loop is infinite

14 Answer 6 The answer is B

15 Question 7 3. Which one of the following is an illegal array declaration? A. int [] a; B. int [] a = new int[0]; C. int [] a = new int[10]; D. int [a] = new int[5]; E. int [] a = new int[112358];

16 Answer 7 The answer is D

17 Question 8 Consider the following code fragment: int i = 1; while (i <= 10) i = i + 2; System.out.println(i); Which one of the following will be printed when the above fragment is run? A. nothing — the program will loop indefinitely B. 9 C. 10 D. 11 E. 12

18 Answer 8 The answer is D

19 Question 9 Consider the following method definition: private void myMethod( String test ) { } Which one of the following is TRUE of the above definition? A. the method is static B. the method accepts an argument of type ‘test’ C. the method returns a null pointer D. the definition is illegal and will cause a compile error E. the method does not return a value

20 Answer 9 The answer is E

21 Question 10 Consider the following code: public class Merry {public static void main(String [] args) {int i = 0; while (i < 3) { if( i == 0 ) System.out.print("Very"); i++; if( i == 1 ) System.out.print("Happy"); if( i == 2 ) System.out.print("Merry"); else System.out.print("Fun!"); i++; } System.out.println("Christmas!"); }

22 Question 10 What one of the following is printed when the above code is executed? A.VeryHappyMerry B. VeryHappyFun!Christmas! C. VeryHappyFun!Fun!Christmas! D.HappyMerryFun! E. VeryChristmas!

23 Answer 10 The answer is C

24 Question 11.Given the following code: int x = 2, y = 3; if (x > 0) { x++; if (x != y); x = 0; y = 0; } System.out.println(x + ", " + y); Which one of the following answers will be printed on standard output? A. 0, 0 B. 2, 3 C. 0, 3 D. 3, 3 E. 2, 0

25 Answer 11 The answer is A

26 Question 12 Write a program in Java that will print the following output on the screen: 0 0 0 0 0 0 0 1 2 3 0 1 3 5 7 0 2 5 8 11

27 Answer 12 public class Main { public static void main(String[] args) { int[][] a = {{0, 0, 0, 0, 0}, {0, 0, 1, 2, 3}, {0, 1, 3, 5, 7}, {0, 2, 5, 8, 11}}; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } }

28 Question 13 In Java, a parent class is referred to in the child class using super.

29 Answer 13 true

30 Question 14 For the Java code f.addActionListener(new A() { void actionPerformed(ActionEvent e) { System.out.println("yup"); } }); to be correct, where f is a Frame, which of the following must be true? a. A is a class implementing the ActionListener interface. b. A is a class implementing the MouseListener interface. c. A is an interface that extends the ActionListener interface. d. None of the above

31 Answer 14 d

32 Question 15 Write a class definition for the following scenario. You have to write a class called MyFraction which works on fractions of the form a/b where a represents the numerator and b represents the denominator. Both a and b are integers (i.e. if a = 1 and b = 2, then the fraction will be 1/2). Your class should perform the following operations: i. Create two constructors – (1) with no parameters that set a = 0 and b = 1; and (2) with 2 integer parameters that sets both a and b. ii. Write an instance method for addition which returns the resultant object. [int: a/b + c/d = (ad + bc)/bd] iii. Write a static method for multiplication which returns the resultant object. [Hint a/b * c/d = ac/bd] iv. Write a static main() that allocates two Fractions, 1/2 and 3/4 and stores their sum in a third variable.

33 Answer 15 class MyFraction { int a, b; MyFraction() { a = 0; b = 1; } MyFraction(int x, int y) { a = x; b = y; } MyFraction addition(MyFraction x) { return new MyFraction((a * x.b + b * x.a), (b * x.b)); } static MyFraction multiplication(MyFraction x, MyFraction y) { return new MyFraction((x.a * y.a), (x.b * y.b)); } public class E6_5 { public static void main(String[] args) { MyFraction f1 = new MyFraction(1, 2); MyFraction f2 = new MyFraction(3, 4); MyFraction f3 = f2.addition(f1); }

34 Question 16 X is a subclass of Y. Does the last two assignments below produce a compile-time error? [2006. Marks: 2] X x = new X(); Y y = new Y(); y = x; x = y;

35 Answer 16 The assignment y = x does not produce any compile-time error as subclass object can be assigned to superclass reference. But the assignment x = y produces a compile-time error.

36 Question 17 What are Wrapper Classes?

37 Answer 17 Wrapper Classes are classes that encapsulate a primitive type within an object.

38 Question 18 Given the following piece of code: public class Company{ public abstract double calculateSalaries(); } which of the following statements is true? a.The keywords public and abstract can not be used together. b.The method calculateSalaries() in class Company must have a body c.You must add a return statement in method calculateSalaries(). d.Class Company must be defined abstract.

39 Answer 18 The answer is d

40 Question 19 Given the following piece of code: public interface Guard{ void doYourJob(); } abstract public class Dog implements Guard{} which of the following statements is correct? a. This code will not compile, because method doYourJob() in interface Guard must be defined abstract. b. This code will not compile, because class Dog must implement method doYourJob() from interface Guard. c. This code will not compile, because in the declaration of class Dog we must use the keyword extends in stead of implements. d. This code will compile without any errors.

41 Answer 19 The answer is d

42 Question 20 What are the steps of the program development cycle ?

43 Answer 20 Edit (write) the program, Compile the program, Execute (run) the program

44 Question21 Write a line of Java code that displays the last digit of an int (for example int n = 1267;)

45 Answer 21 A. System.out.println(n%10);

46 QUIZ 1 What is displayed when the following code is compiled and executed? String s1 = new String("Test"); String s2 = new String("Test"); if (s1==s2) System.out.println("Same"); if (s1.equals(s2)) System.out.println("Equals"); A. Same Equals B. Equals C. Same D. The code compiles, but nothing is displayed upon execution. E. The code fails to compile.

47 Quiz 2 What is the result when you compile and run the following code? public class Test { public void method() { for(int i = 0; i < 3; i++) System.out.print(i); } A. 0122 B. 0123 C. Compilation error D.None of these

48 Bquiz1 CQUIZ 2


Download ppt "Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;"

Similar presentations


Ads by Google