Monday, November 17, 2008

Object Oriented Programming and Java (MC221) : April 2006

Section A : Basic Concepts (30 Marks)
1. The default value of a static integer variable of a class in Java is,
(a) 0 (b) 1 (c) Garbage value (d) Null (e) -1.
2. What will be printed as the output of the following program?
public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println("I = " +i);
}
}
(a) I = 0 (b) I = 1 (c) I = 2 (d) I = 3 (e) Compile-time Error.
3. Multiple inheritance means,
(a) one class inheriting from more super classes
(b) more classes inheriting from one super class
(c) more classes inheriting from more super classes
(d) None of the above
(e) (a) and (b) above.
4. Which statement is not true in java language?
(a) A public member of a class can be accessed in all the packages.
(b) A private member of a class cannot be accessed by the methods of the same class.
(c) A private member of a class cannot be accessed from its derived class.
(d) A protected member of a class can be accessed from its derived class.
(e) None of the above.
5. To prevent any method from overriding, we declare the method as,
(a) static (b) const (c) final (d) abstract (e) none of the above.
6. Which one of the following is not true?
(a) A class containing abstract methods is called an abstract class.
(b) Abstract methods should be implemented in the derived class.
(c) An abstract class cannot have non-abstract methods.
(d) A class must be qualified as ‘abstract’ class, if it contains one abstract method.
(e) None of the above.
7. The fields in an interface are implicitly specified as,
(a) static only (b) protected (c) private (d) both static and final (e) none of the above.
8. What is the output of the following program:
public class testmeth
{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+” , “);
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
(a) 1 , 3 (b) 3 , 1 (c) 1 , 1 (d) 1 , 0 (e) none of the above.
9. Which of the following is not true?
(a) An interface can extend another interface.
(b) A class which is implementing an interface must implement all the methods of the interface.
(c) An interface can implement another interface.
(d) An interface is a solution for multiple inheritance in java.
(e) None of the above.
10. Which of the following is true?
(a) A finally block is executed before the catch block but after the try block.
(b) A finally block is executed, only after the catch block is executed.
(c) A finally block is executed whether an exception is thrown or not.
(d) A finally block is executed, only if an exception occurs.
(e) None of the above.
11. Among these expressions, which is(are) of type String?
(a) "0" (b) "ab" + "cd" (c) '0' (d) Both (A) and (B) above (e) (A), (B) and (C) above.
12. Consider the following code fragment
Rectangle r1 = new Rectangle();
r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);
After the above piece of code is executed, what are the colors of r1 and r2 (in this order)?
(a) Color.blue , Color.red (b) Color.blue , Color.blue (c) Color.red , Color.red
(d) Color.red , Color.blue (e) None of the above.
13. What is the type and value of the following expression? (Notice the integer division)
-4 + 1/2 + 2*-3 + 5.0
(a) int -5 (b) double -4.5 (c) int -4 (d) double -5.0 (e) None of the above.
14. What is printed by the following statement?
System.out.print("Hello,\nworld!");
(a) Hello, \nworld! (b) Hello, world! (c) "Hello, \nworld!" (d) None of the above.
15. Consider the two methods (within the same class)
public static int foo(int a, String s)
{
s = "Yellow";
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = "Blue";
a = foo(a,s);
System.out.println("a="+a+" s="+s);
}
public static void main(String args[])
{
bar();
}
What is printed on execution of these methods?
(a) a = 3 s = Blue (b) a = 5 s = Yellow (c) a = 3 s = Yellow (d) a = 5 s = Blue (e) none of the above.
16. Which of the following variable declaration would NOT compile in a java program?
(a) int var; (b) int VAR; (c) int var1; (d) int var_1; (e) int 1_var;.
17. Consider the following class definition:
public class MyClass
{
private int value;
public void setValue(int i){ /* code */ }
// Other methods...
}
The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue?
(a) value = i; (b) this.value = i; (c) value == i; (d) Both (A) and (B) and above (e) (A), (B) and (C) above.
18. Which of the following is TRUE?
(a) In java, an instance field declared public generates a compilation error.
(b) int is the name of a class available in the package java.lang
(c) Instance variable names may only contain letters and digits.
(d) A class has always a constructor (possibly automatically supplied by the java compiler).
(e) The more comments in a program, the faster the program runs.
19. A constructor
(a) Must have the same name as the class it is declared within.
(b) Is used to create objects.
(c) May be declared private
(d) Both (A) and (B) above
(e) (a), (b) and (c) above.
20. Consider,
public class MyClass
{
public MyClass(){/*code*/}
// more code...
}
To instantiate MyClass, you would write?
(a) MyClass mc = new MyClass();
(b) MyClass mc = MyClass();
(c) MyClass mc = MyClass;
(d) MyClass mc = new MyClass;
(e) The constructor of MyClass should be defined as, public void MyClass(){/*code*/}.
21. What is byte code in the context of Java?
(a) The type of code generated by a Java compiler.
(b) The type of code generated by a Java Virtual Machine.
(c) It is another name for a Java source file.
(d) It is the code written within the instance methods of a class.
(e) It is another name for comments written within a program.
22. What is garbage collection in the context of Java?
(a) The operating system periodically deletes all the java files available on the system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is automatically
reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that doesn't make sense.
(e) Janitors working for Sun Micro Systems are required to throw away any Microsoft documentation found in the employees' offices.
23. You read the following statement in a Java program that compiles and executes.
submarine.dive(depth); What can you say for sure?
(a) depth must be an int
(b) dive must be a method.
(c) dive must be the name of an instance field.
(d) submarine must be the name of a class
(e) submarine must be a method.
24. The java run time system automatically calls this method while garbage collection.
(a) finalizer() (b) finalize() (c) finally() (d) finalized() (e) none of the above.
25. The correct order of the declarations in a Java program is,
(a) Package declaration, import statement, class declaration
(b) Import statement, package declaration, class declaration
(c) Import statement, class declaration, package declaration
(d) Class declaration, import statement, package declaration
(e) Class declaration, package declaration, import statement.
26. An overloaded method consists of,
(a) The same method name with different types of parameters
(b) The same method name with different number of parameters
(c) The same method name and same number and type of parameters with different return type
(d) Both (a) and (b) above
(e) (a), (b) and (c) above.
27. A protected member can be accessed in,
(a) a subclass of the same package (b) a non-subclass of the same package
(c) a non-subclass of differet package (d) a subclass of different package
(e) the same class.
Which is the false option?
28. What is the output of the following code:
class eq
{
public static void main(String args[])
{
String s1 = “Hello”;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}
(a) true (b) false (c) 0 (d) 1 (e) Hello.
29. All exception types are subclasses of the built-in class
(a) Exception (b) RuntimeException (c) Error (d) Throwable (e) None of the above.
30. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the
(a) Super class (b) Subclass (c) Compiler will choose randomly
(d) Interpreter will choose randomly (e) None of the abvove.
Section B : Problems (50 Marks)
1. Write a Java program to implement a bank account having the methods deposit(), withdraw(), and showbalance() with appropriate parameters. (10 marks)
2. Write a Java program to implement the method overloading concept for a problem, where some students submit directly character grades and some students submit marks for which we have to evaluate grades. The method accepts a student’s marks or grades and finally stored as character grades. (10 marks)
3. Create a super class ‘Shapes’. Create subclasses for different shapes ‘Rectangle’, ‘Square’, ‘Circle’, and for each of them calculate area and display values with common methods.
(10 marks)
4. Write a Java program to demonstrate at least five mouse event handlers. (10 marks)
5. Write a Java program to demonstrate key event handlers. (10 marks)
Section C : Applied Theory (20 Marks)
6. What is multithreading? Explain the two ways to implement multithreading in Java. Also explain the thread states. (10 marks)
7. What is meant by inheritance? Explain the types of inheritance. What is an Interface? Explain it. (10 marks)
Suggested Answers
1. Answer : (a)
Reason: The default value of a static integer variable of a class in Java is 0.
2. Answer : (b)
Reason: 1
The execution goes on like this:
int i = 0; // i becomes 0
i = 0 + i; // now, i becomes 1
i = 0 + 1; // perform addition and assign 1 to i.
3. Answer : (a)
Reason: Multiple inheritance means one class inheriting from more super classes.
4. Answer : (b)
Reason: Private members of a class can be accessed by the methods within the same class.
5. Answer : (c)
Reason: Final methods of the base class cannot be overridden in the derived Class.
6. Answer : (c)
Reason: An abstract class can contain both abstract and non-abstract methods.
7. Answer : (d)
Reason: The fields in an interface are implicitly specified as both static and final.
8. Answer : (c)
Reason: Parameter values are passed by value in the calling of a method, and so a copy of the value is created in the method, and the original value is not affected by the method call.
9. Answer : (c)
Reason: An interface can extend another interface but not implement.
10. Answer : (c)
Reason: A finally block is executed whether an exception is thrown or not is correct.
11. Answer : (d)
Reason: Strings are "0" and "ab" + "cd" .
12. Answer : (c)
Reason: Both r1 and r2 are referring the same object of Rectangle class. So, finally the Color of the object is changed to red.
13. Answer : (d)
Reason: The execution goes on like this:
-4 + 1/2 + 2*-3 + 5.0;
-4 + 0 + -6 + 5.0; // integer division: 1/2 truncates .5
-10 + 5.0; // higher type is double 5.0, so -10 is casted to
double
-5.0; // finally, double -5.0.
14. Answer : (c)
Reason: The statement
System.out.print("Hello,\nworld!");
gives output as
Hello,
world!
15. Answer : (d)
Reason: ‘a’ value is returned from the method and so it is 5. But the string will remain same, as it is passed by value to the method.
16. Answer : (e)
Reason: The first character of a variable name should not be a digit.
17. Answer : (d)
Reason: ‘==’ is a comparison operator.
18. Answer : (d)
Reason: A class will always have a constructor, either provided by the user or a default constructor provided by the compiler.
19. Answer : (e)
Reason: A constructor
• Must have the same name as the class it is declared within.
• Is used to create objects.
• May be declared private.
20. Answer : (a)
Reason: An object is created by using a new operator.
21. Answer : (a)
Reason: Java compiler compiles the source code file and converts it into a class file, which consists of byte code.
22. Answer : (c)
Reason: Garbage collection in the context of Java is when all references to an object are gone, the memory used by the object is automatically reclaimed.
23. Answer : (b)
Reason: The other choices can be allowed, but not ‘must’
24. Answer : (b)
Reason: ‘finalize()’ method is automatically called by the java compiler before destroying the object to free any resources.
25. Answer : (a)
Reason: First the package name is defined. Then the import statements if any. And then the class declaration goes on.
26. Answer : (d)
Reason: Even though the return type varies, it will not be considered for overloading concept. Only method name and parameters considered.
27. Answer : (c)
Reason: A protected member cannot be accessed in non-subclasses of different packages.
28. Answer : (b)
Reason: Since , the contents of the two String objects are identical, But they are distinct objects.
29. Answer : (d)
Reason: Throwable is the super class of all the exception classes.
30. Answer : (b)
Reason: The compiler will always selects the version of the methodin subclass. superclass method can be called by explicitly
assigning super reference.

No comments: