Monday, November 17, 2008

Object Oriented Programming and Java (MC221): July 2005

Section A : Basic Concepts (30 Marks)
1. A process that involves recognizing and focusing on the important characteristics of a situation or object is known as:
(a) Encapsulation (b) Polymorphism (c) Abstraction (d) Inheritance
(e) Object persistence.
2. Which statement is true regarding an object?
(a) An object is what classes instantiated are from
(b) An object is an instance of a class
(c) An object is a variable
(d) An object is a reference to an attribute
(e) An object is not an instance of a class.
3. In object oriented programming, composition relates to
(a) The use of consistent coding conventions
(b) The organization of components interacting to achieve a coherent, common behavior
(c) The use of inheritance to achieve polymorphic behavior
(d) The organization of components interacting not to achieve a coherent common behavior
(e) The use of data hiding to achieve polymorphic behavior.
4. In object oriented programming new classes can be defined by extending existing classes. This is an example of:
(a) Encapsulation (b) Interface (c) Composition (d) Inheritance (e) Aggregation.
5. Which of the following does not belong: If a class inherits from some other class, it should
(a) Make use of the parent class's capabilities
(b) Over-ride or add the minimum to accomplish the derived class' purpose
(c) Over-ride all the methods of its parent class
(d) Make sure the result "IS-A-KIND-OF" its base class
(e) Make sure the result “contains” its base class.
6. Object oriented inheritance models the
(a) "is a kind of" relationship
(b) "has a" relationship
(c) "want to be" relationship
(d) inheritance does not describe any kind of relationship between classes
(e) “contains” of relationship.
7. The wrapping up of data and functions into a single unit is called
(a) Encapsulation (b) Abstraction (c) Data Hiding (d) Polymorphism (e) Message passing.
8. Polymorphism
(a) Is not supported by Java
(b) Refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class-specific ways
(c) Simplifies code maintenance
(d) Not simplifies code manintenance
(e) Refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class –specific ways and simplifies code maintenance.
9. In object oriented programming new classes can be defined by extending existing classes. This is an example of:
(a) Encapsulation (b) Interface (c) Composition (d) Inheritance (e) Aggregation.
10. Given a class named student, which of the following is a valid constructor declaration for the class?
(a) Student (student s) { } (b) Student student ( ) { }
(c) Private final student ( ) { } (d) Void student ( ) { }
(e) Static void student(){ }.
11. What is garbage collection in the context of Java?
(a) The operating system periodically deletes all of 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) When all references to an object are gone the memory used by the object is not reclaimed.
12. The concept of multiple inheritance is implemented in Java by
I. Extending two or more classes.
II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
(a) Only (II) (b) (I) and (II) (c) (II) and (III)
(d) Only (I) (e) Only (III).
13. In Java, declaring a class abstract is useful
(a) To prevent developers from further extending the class
(b) When it doesn't make sense to have objects of that class
(c) When default implementations of some methods are not desirable
(d) To force developers to extend the class not to use its capabilities
(e) When it makes sense to have objects of that class.
14. What is the error in the following class definitions?
Abstract class xy
{
abstract sum (int x, int y) { }
}
(a) Class header is not defined properly.
(b) Constructor is not defined.
(c) Method is not defined properly
(d) Method is defined properly
(e) No error.
15. Which of these field declarations are legal within the body of an interface?
(a) Private final static int answer = 42 (b) public static int answer=42
(c) final static answer =42 (d) int answer (e) No error.
16. A package is a collection of
(a) Classes (b) Interfaces (c) Editing tools (d) Classes and interfaces (e) Editing tools and interfaces.
17. A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?
(a) Declare the method with the keyword public
(b) Declare the method with the keyword private
(c) Declare the method with the keyword protected
(d) Do not declare the method with any accessibility modifiers
(e) Declare the method with the keyword public and private.
18. Basic Java language functions are stored in which of the following java package?
(a) java.lang (b) java.io (c) java.net (d) java.util (e) java.awt.
19. Which of the following is a member of the java.lang package?
(a) List (b) Queue (c) Math (d) Stack (e) Process.
20. Which of the following has a method names flush( )?
(a) Input stream (b) Output Stream (c) Reader stream (d) Input reader stream
(e) Input output stream.
21. What is the fundamental unit of information of writer streams?
(a) Characters (b) Bytes (c) Files (d) Records (e) Information.
22. File class is included in which package?
(a) java.io package (b) java.lang package (c) java.awt package (d) java.net.package
(e) java.util.package.
23. Given the code
String s1 = “ yes” ;
String s2 = “ yes “ ;
String s3 = new String ( s1);
Which of the following would equate to true?
(a) s1 == s2 (b) s1 = s2 (c) s3 == s1 (d) s3=s1 (e) s1!=s2.
24. URL stands for
(a) Universal reader locator (b) Universal reform loader (c) Uniform resource loader (d) Uniform resource locator (e) Uniform reader locator.
25. What is the sequence of major events in the life of an applet?
(a) init, start, stop, destroy (b) start, init , stop , destroy
(c) init, start , destroy, stop (d) init, start, destroy
(e) destroy, start, init, stop.
26. Which of the following is true in regard to applet execution?
(a) Applets loaded from the same computer where they are executing have the same restrictions as applets loaded from the network.
(b) Applets loaded and executing locally have none of the restrictions faced by applets that get loaded from the network.
(c) Applets loaded and executing locally have some restrictions faced by applets that get loaded from the network.
(d) Applets cant react to user input and change dynamically
(e) Applets can be run independently.
27. What is the return type of the method getID() defined in AWTEvent class
(a) Int (b) long (c) Object (d) Component (e) float.
28. Which of the following events will cause a thread to die?
(a) The method sleep( ) is called
(b) The method wait( ) is called
(c) Execution of the start( ) method ends
(d) Execution of the run( ) method ends
(e) Execution of the run() method is called.
29. What will be the result of the expression 13 & 25?
(a) 38 (b) 25 (c) 9 (d) 12 (e) 21.
30. Which of the following statements are true regarding the finalize( ) method?
(a) The finalize ( ) method must be declared with protected accessibility
(b) The compiler will fail to compile the code that explicitly tries to call the finalize( ) method
(c) The body of the finalize ( ) method can only access other objects that are eligible for garbage
collection
(d) The finalize ( ) method can be overloaded
(e) The finalize() method cant be overloaded.
Section B : Problems (50 Marks)
1. a. Why is Java known as platform-neutral language? Explain the importance of JVM in Java.
b. What do you mean by dynamic initialization of a variable? Give an example. (5 + 5 = 10 marks)
2. a. What are objects? How are they created from a class?
b. What is a constructor? What are its special properties? (5 + 5 = 10 marks)
3. a. What is meant by method overriding? What is the difference between methods overloading from method overriding.
b. Distinguish between static and dynamic polymorphism. (5 + 5 = 10 marks)
4. a. Define Applet and give the syntax of the applet tag in an HTML file.
b. Write a short note on the following:
i. IP Address.
ii. URL. (2 + 8 = 10 marks)
5. a. What is JDBC? How do you connect to the Database?
b. Give the sequence of steps to invoke a SQL query using JDBC. (5 + 5 = 10 marks)
Section C : Applied Theory (20 Marks)
6. a. Given below are two files :
File Employee.java
package purchase;
public class Employee
{
protected double age = 35;
}
import purchase.Employee;
public class Company
{
public static void main (String args[ ] )
{
Employee e= new Employee ( );
System.out.println ( “Age = “ + e.age );
}
}
Will the file Company.java compile? Explain the reason briefly.
b. Consider the following class file :
import java.awt.*;
import java.io.*;
package studentbase;
class Test
{
void display ( )
{
System.out.println (“Results”);
}
}
Will it compile? Give reason for the answer.
c. How will the following program lay out its buttons.
import java.awt.*;
public class MyClass
{
Public static void main(String args[])
{
String [] labels = {"A","B","C","D","E","F"};
Window win = new Frame ();
win.setLayout(new GridLayout(1,0,2,3));
for(int i=0;i < labels.length;i++)
win.add(new Button(labels[i]));
win.pack ();
win.setVisible (true);
}
}
d. Is it possible to have multiple catch blocks? Give reason and Illustrate with an example.
(6 + 2 + 2 + 10 = 20 marks)
Suggested Answers
1. Answer : (c)
Reason : The insulation of data from direct access by the program is called as the
data abstraction.Data abstraction also called as data hiding, is the mechanism where by the code and the data it manipulates are wrapped into a single unit.
2. Answer : (b)
Reason : An object is an instance of a class. Objects are created from class definitions that implement abstractions.
3. Answer : (b)
Reason : Composition is about an object that is made up of other objects, referred as "containment. Composition implies that the life cycles are more strongly linked, that means Whole is responsible for the life time of Part.
4. Answer : (d)
Reason : Inheritance is a mechanism that enables one class to inherit all of the behaviour (methods ) and attributes (instance variables) of another class. A class that inherits from another class is called a subclass and the class that gives the inheritance is called a superclass.
5. Answer : (c)
Reason : In a class hierarchy, when a method in a subclass has the same method signature as that of the superclass, then the method in the subclass is said to “override” the method in superclass. When an overridden method exists in the subclass, the subclass objects always refer its own. The subclass overridden method hides ( or blocks ) that of the superclass method.
6. Answer : (a)
Reason : As it models generalization relationship it takes “is a kind of “keyword.
7. Answer : (a)
Reason : Data encapsulation is the method of wrapping up of data and functions into a single unit.
8. Answer : (e)
Reason : As Polymorphism refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class-specific ways. It also simplifies code manintenance.
9. Answer : (d)
Reason : As inheritance is nothing but extending existing classes whereas the others are for different purpose.
10. Answer : (a)
Reason : A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
11. Answer : (c)
Reason : As the remaining all contradict the functionality of JVM.
12. Answer : (c)
Reason : The remaning are not opt solutions for the concept of multiple inheritance.
13. Answer : (b)
Reason : As other Choices are contradicting the meaning of Abstract Class .
14. Answer : (c)
Reason : method is not defined properly.
15. Answer : (b)
Reason : The remaining are all illegal.
16. Answer : (d)
Reason : Editing tools are not the part of packages.So d is the correct Answer.
17. Answer : (d)
Reason : The desired accessibility is package accessibility, which is the default accessibility for members that have no accessibility modifier. Package is not an accessibility modifier.
18. Answer : (a)
Reason : The basic language functions are stored in a package inside of the java package called java.lang.
19. Answer : (b)
Reason : Math is a member of the java.lang package whereas the remaining are not.
20. Answer : (b)
Reason : Output stream has the method flush ( ) that flushes the output stream.
21. Answer : (a)
Reason : Writer streams uses characters as their fundamental unit of information.
22. Answer : (a)
Reason : File class is included in inputoutput
23. Answer : (a)
Reason : == is a comparision operator whereas = is an assignment operator.
24. Answer : (d)
Reason : The abbreviation of URL uniform resource locator.
25. Answer : (a)
Reason : The remaining all are incorrect because the sequence of major events are wrong.
26. Answer : (b)
Reason : The remaining Choices are contradicting the properties of applets.
27. Answer : (a)
Reason : Int is the return type of the method getID() in AWTEvent Class.
28. Answer : (d)
Reason : A thread dies when the execution of the run( ) method ends. The call to the start method is asynchronous, that is, it returns immediately, and it enables the thread for running. Calling the sleep( ) or wait( ) methods will only block the thread temporarily.
29. Answer : (c)
Reason : 11001&1101==9;
30. Answer : (d)
Reason : The finalize( ) method like any method can be called explicitly if it is accessible. Intended purpose of the method is to be called by the garbage collector in order to clean up before an object is destroyed. Overloading the finalize ( ) method is allowed, but only the method with the original signature will be called by the garbage collector.

No comments: