Csi 321 JAVA Mid
Csi 321 JAVA Mid
Csi 321 JAVA Mid
There are 4 (Four) questions. Answer any 3 (Three) of them. Figures in the right margin indicate marks. 1. a) Define Java Virtual Machine (JVM) and bytecodes. Differentiate the Right Shift Operator (>>) from the Unsigned Right Shift Operator (>>>) with an appropriate example. b) Consider the following code snippet.
int i = 10; int n = i++%5;
3.5
2.5
What are the values of i and n after the code is executed? What are the final values of i and n if instead of using the postfix increment operator (i++), you use the prefix version (++i)? Explain your answer. Note: Your answer will NOT carry any mark if the explanation is incorrect. c) State the output of following Java code segment:
public class StringDemo { public static void main(String[] args) { String ori = "Hello Java!"; StringBuffer res = new StringBuffer(" hello "); int index = ori.lastIndexOf('a'); System.out.println(index); for(int i=0; i<ori.length()/2; i++){ res.insert(i,ori.charAt(i)); res.append(ori.substring(ori.length()-i)); System.out.println(res); } } }
I. II.
2.
a) What is constructor overloading? Write down the output of following code segment:
public class Cube { static int n = 1; int side; Cube(int n){side = n;} int getVolume(){return side*side*side;} public static void main(String[] args) { Cube[] cubes = new Cube[5]; Cube cube = new Cube(n); for(int i=0; i<cubes.length; i++){ cubes[i] = cube; n++; cubes[i].side = n; System.out.println(cubes[i].getVolume()); }
Page1of3
Now complete the Matrix class by providing syntactically correct code according to the instructions given below: i. In constructor initialize all matrix elements with random numbers. Size of the matrix must be defined by using the values of passed-in arguments (height & length). ii. Write code to print the matrix in printMatrix() method. iii. Add the matrix with passed-in matrix Matrix m1 in addMatrix() method. You must return the resultant matrix and check all necessary conditions. iv. In main method write proper code to declare and initialize 2 (Two) 3 X 3 Matrices. Then add these two matrices using addMatrix method. Finally print the resultant matrix from main method. 3. a) The following program doesn't compile. What do you need to do to make it compile? Why?
public class Problem { String s; static class Inner { void testMethod() { s = "Set from Inner"; } } }
2.5
2.5
c) Write proper code to construct a static Java method named getAverage. Use varargs to pass an arbitrary number of integers to the method getAverage. The method will calculate the average (arithmetic mean) of all passed-in integers and return the average as a floating point number.
Page2of3
2.5
2.5
4.
a) What are Method Overriding and Method Hiding? Explain with an appropriate example. b) What is the difference between public and protected access modifier? c) Briefly describe different uses of abstract and final keywords in Java. d) What is the output of following code? (Consider all the classes are in same package)
public class SuperClass { static int a,b; static{ a = b = 2; for(int i=0; i<3; i++) b+= a; } int num1 = 5; SuperClass(){ a = 5;} SuperClass(int n){ a = n;} void method1(){ System.out.println("Super method1");} static void method2(){ System.out.println("Super method2");} } public class SubClass extends SuperClass { SubClass(){ this (10); a = 15;} SubClass(int n){ super(n); a = 20;} void method1(){ System.out.println("Sub method1");} static void method2(){ System.out.println("Sub method2");} public static void main(String[] args) { SuperClass sup = new SuperClass(); SubClass sub = new SubClass(); sup = sub; sub.method1(); sup.method1(); SuperClass.method2(); SubClass.method2(); System.out.println (sub.num1+" "+SuperClass.a+" "+SuperClass.b); } }
2 1 3 4
Page3of3