First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int a = 8; a = a/2; a. Other b. 12 c. 4 d. 0 40 Marks 2. What is the value of a[99] after these statements? int[] a = new int[100]; a[0] = 1; for (int i = 1; i < a.length; i++) a[i] = 1 - a[i-1]; (a) -2 (b) -1 (c) 0 (d) 1 (e) 2 B) Find and correct the error in each of the following program segments: a) final int MAX = 10; MAX = 15; b) Assume int b[] = new int[ 10 ]; for ( int i = 0; i <= b.length; i++ ) b[ i ] = 1; c) Assume int a[][] = { { 1, 2, { 3, 4 ; a[ 1, 1 ] = 5; d) public class C { private int p; public C( ) { System.out.println("C's no-arg constructor invoked"); this(0); public C(int p) { p = p; public void setp(int p) { p = p; 1/7
C )What will be the return value if we call f(7); int f (int v) { if ((v = = 1) (v = = 0)) return 1; else if ((v%2) = = 0) return f(v/2) + 2; else return f(v-1) + 3; D) What is the output of the following program? public class Foo { private static int i = 0; private static int j = 0; public static void main(string[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); E) Read the following code: Can you run the code without errors? if no find the error and correct it. What is the output of the code? And what is the relation between class A and B? class A { int data; public void f(int arg) { data = arg; public int g() { return data; Public class B { A x; public class Test{ public static void main(string args[]) { B obj; obj.x.f(20); system.out.println( obj.x.g()); system.out.println (obj.g()); 2/7
Second Question No. of Branches (5) 40 marks 1. What is reusability? How do you achieve this in Java? 2. What is overloading? Overload a method called Sum which can take two or three integers and returns their summation. Write just two lines to call each of them with values. 3. Write a recursive function called SumRange that takes two arguments n and m and returns the sum of all integers from n to m, both inclusive. 3/7
4. Write a method called search that takes two parameters a 2D array of integers and an integer value n. It should return true if the element n is found in the array, false otherwise. 5. What is the difference between call by value and call by reference? Give example for each? 4/7
Third Question: No. of Branches (2) 20 Marks The following tree shows a general Person class with its main properties. Try to understand it carefully and then answer the following questions. A Person has a Job : Person Job { role; salary; id Person {name, age Use Composition Name Age Job Role Salary ID 1- Write a complete object oriented JAVA program to implement the above tree, 2- The Person class might have the following operations: A constructor Person Data Input operation (Set, Get) Person Total Salary for one year (calculation operation.) Person Information Display operation. (Outut) 3- The main program might: Create a Person object; Apply all operations for the object. 5/7
Good Luck 6/7
7/7