Object Oriented Programming and Java (MC221) July 2005
Object Oriented Programming and Java (MC221) July 2005
1.
Encapsulation
Abstraction
Object persistence.
(b) Interface
(d) Inheritance
This is an
(e) Aggregation.
Which of the following does not belong: If a class inherits from some other class, it should
7.
In object oriented programming new classes can be defined by extending existing classes.
example of:
(a)
(b)
(c)
(d)
(e)
6.
(a) Encapsulation
(c) Composition
5.
(b) Polymorphism
(d) Inheritance
4.
3.
A process that involves recognizing and focusing on the important characteristics of a situation or object
is known as:
(a)
(c)
(e)
2.
(a) Encapsulation
(c) Data Hiding
8.
(b) Abstraction
(d) Polymorphism
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.
example of:
(a) Encapsulation
(d) Inheritance
(b) Interface
(e) Aggregation.
This is an
(c) Composition
10. Given a class named student, which of the following is a valid constructor declaration for the class?
(a) Student (student s) { }
(c) Private final student ( ) { }
(e) Static void student(){ }.
15. Which of these field declarations are legal within the body of an interface?
(a) Private final static int answer = 42
(c) final static answer =42
(e) No error.
(b) Interfaces
(c) Editing tools
(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)
(b)
(c)
(d)
(e)
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.
< Answer >
(b) Queue
(c) Math
(d) Stack
(e) Process.
< Answer >
(b) Bytes
(d) Records
(e) Information.
< Answer >
(b) s1 = s2
(c) s3 == s1
(d) s3=s1
(e) s1!=s2.
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.
< Answer >
27. What is the return type of the method getID() defined in AWTEvent class
(a) Int
(b) long
(c) Object
(d) Component
(e) float.
< Answer >
(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.
END OF SECTION A
a.
Why is Java known as platform-neutral language? Explain the importance of JVM in Java.
b.
2.
a.
b.
3.
a.
What is meant by method overriding? What is the difference between methods overloading from method
overriding.
b.
a.
b.
i.
ii.
Define Applet and give the syntax of the applet tag in an HTML file.
Write a short note on the following:
IP Address.
URL.
a.
b.
END OF SECTION B
6.
a.
}
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.
{
System.out.println (Results);
}
}
Will it compile? Give reason for the answer.
c.
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) < Answer >
END OF SECTION C
END OF QUESTION PAPER
Suggested Answers
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.
11.
Answer : (c)
Reason : As the remaining all contradict the functionality of JVM.
Section B : Problems
1.
a.
Java is known as platform-neutral language as it is not tied to any particular hardware or operating system.
Java programs can be executed any where on any system. They can be moved from one computer system to
another, anywhere and anytime. Changes and upgrades in operating systems, processors and system
resources will not force any changes in the programs.
The Java Virtual Machine is the engine that actually executes a Java program. When a Java program is run,
the instructions are not executed directly by the hardware of the local system, instead an interpreter or
"virtual processor" walks through the instructions step by step and carries out the action the instruction
represents. Java compiler produces and intermediate code known as bytecode .
The machine specific code is generated by the Java interpreter by acting as an intermediary between the byte
code and the real machine as shown below :
b.
Dynamic initialization is the method of initializing the variables dynamically at runtime using any
expression valid at the time the variable is declared .
For example, here is a short program that computes the area of a circle given the radius of it :
// Dynamic initialization
Class DynInit {
Public static void main (String args { } ) {
double r = 5.0;
double p=3.14
// a is dynamically initialized
double a = pi * (r * r )
System.out.println (Area is , + a );
}
}
Here, three local variables are used- r, p, a are declared. The first two r, p, a re initialized by constants.
However, a is initialized dynamically to the area of the circle. Initialization expression may use any element
valid at the time of the initialization, including calls to methods, other variables, or literals.
< TOP >
2.
a.
An object is a specific instance of a class. An object in java is essentially a block of memory that contains
space to store all the instance variables.
The process of creating objects from a class is called instantiation. The object is created using the class and is
a concrete instance of the abstraction that the class represents. An object must be created before it can be
used in a program. In Java objects are manipulated through object references. The process of creating objects
involves the following steps:
1.
2.
assigns
b.
This first statement declares a variable to hold the object reference and the second one actually
the object reference to the variable.
A constructor is a method, which is invoked when an object of a class is created. It has no return type in java.
Its name must be same as the name of class. It can accept parameter. Here we look at how they function in
java.
Class student
{
int age;
string name;
student( ) //constructor
{
age=0;
name="kiran";
}
In java the overloading of constructors is possible. An overloading means using same name of constructor
with different parameters. An example is given.
Class student
{
int age;
string name;
student() //constructor
{
age=0;
name="kiran";
}
student(int n, string s) // constructor overloading
{
age=n;
name=s;
}
Properties:
Constructors enable an object to initialize itself when created.
Constructors have the same name as that of the class.
They do not specify a return type, not even void.
They return the instance of the class itself
< TOP >
3.
a.
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 object always refer its own. The subclass overridden method hides ( or
blocks ) that of the superclass method.
Example :
Class X
{
int i,j;
X ( int a , int b)
{
i = a;
j = b;
}
void show( )
{
System.out.println ( i and j : + i + + j );
}
}
Class Y extends X
{
int k;
Y ( int a , int b , int c )
{
super ( a, b );
k = c;
}
void show ( )
{
System.out.println ( k : + k );
}
}
Class Overridedemo
{
public static void main (String args [ ] )
{
Y subob = new Y ( 1,2,3 );
subob. Show ( );
}
}
Output :
k:3
When show( ) is invoked on an object of type Y, the version of the show( ) defined in Y is used. That is, the
version show ( ) inside Y overrides the version declared in X.
b.
Static polymorphism : In static polymorphism, which method is to be called is decided at compile time only.
Method overloading is an example of static polymorphism. In Method overloading the same method name is used
many times in the same class, but with different parameters. Depending on the parameters passed, it is decided at
compile as which method is to be called. Parameters are the deciding factor (not return type) for the compiler to
decide which method is to be called actually at run-time. In static polymorphism, decision is take at compile-time
(contrast with dynamic polymorphism).
Dynamic polymorphism (also called dynamic method dispatch) is the mechanism by which a call to an overridden
function is resolved at run-time ( not at compile time). Dynamic method dispatch is the method by which Java
implements run-time polymorphism.
When an overridden method is called through a superclass reference, Java
determines which version of that method is to be executed based upon the type of the object being referred at the time the
call occurs.
Thus this determination is made at run time. When different type of objects are referred to , different
versions of an overridden method will be called. It is the type of object being referred to (not the type of reference
variable) that determines which version of an overridden method will be executed. Therefore, if a superclass
contains a method that is overridden by a subclass, then , when different types of objects are referred to through a
superclass reference variable, different versions of the methods are executed.
< TOP >
4.
a.
Java applets are compiled Java programs that are stored on a Web server. A Java applet is a special kind of
program because it can't run on its own. It needs a Web browser to give it a home. When your Web browser
encounters a reference to a Java applet in a Web page, it sends a request to the server that gave it the Web
page to send along the compiled code for the applet as well.
The APPLET tag is used to start an applet from both an HTML document and from an applet viewer. An applet
viewer will execute each APPLET tag that it finds in a separate window, while web browsers like Netscape
Navigator, Internet Explorer, and Hot Java will allow many applets on a single page.
The syntax for the standard APPLET tag is shown here. The bracketed items are optional.
< APPLET
[ CODE BASE = Codebase URL ]
CODE = appletFile
[ ALT = alternateText]
[ Name = applet Instance Name ]
WIDTH = pixels HEIGHT = pixels
[ALIGN = alignment ]
[VSPACE = pixels ] [ HSPACE = pixels ]
>
[ <PARAM NAME = Attribute Name VALUE = Attribute Value > ]
[ <PARAM NAME = Attribute Name2 VALUE = Attribute Value > ]
a.
net.
An IP address (Internet Protocol Address) is a number that uniquely identifies each computer on the
All internet address consists of 32-bit values (called Ipv4 Internet Protocol, version 4).Latest
addressing scheme, called IPv 6 (Internet Protocol, version 6) uses a 128-bit value to represent an
address. It supports larger address space.
For example, 1.160.10.240 could be an IP address.
b.
URL is an acronym for Uniform Resource Locator and is a reference (an address) to a
resource on
the Internet. The following is an example of a URL which addresses the Java Web
site hosted by
Sun Microsystems:
Reference : A reference to a named anchor within a resource that usually identifies a specific location
within a file (typically optional).
< TOP >
5.
a.
2.
3.
4.
6.
7.
8.
b.
a.
No, the file Company.java will not compile. The field age in the Employee class should be declared as public.
The three access specifiers, private, public and protected, provide a variety of ways to produce
many levels of access required.
Any thing declared public can be accessed from any where. Anything declared private cannot be
seen outside of its class. When a member does not have an explicit access specification , it is
visible to subclasses as well as to other classes in the same package. This is default access. When
an element is to be seen outside the current package, but only to classes that subclass the class
directly, then declare that element protected.
Class members Access
b.
c.
d.
Private
No modifier
Protected
Public
Same class
Yes
Yes
Yes
Yes
Same package
subclass
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Different package
subclass
No
No
Yes
Yes
Different package
non-subclass
No
No
No
Yes
No, The package definition must come first. The given problem is contradicting to the definition of a
package.
After careful observation of the program the output of the code is that its displays all buttons in a single
row.
There may be situations where more than one exception could be raised by a single piece of code. To handle
this type of situation, we can specify two or more catch blocks, each catching a different type of exception.
When an exception is thrown, each catch statement is inspected in order, and the first one whose type
matches with that of the exception thrown is executed. If one catch block is executed, the others are
bypassed, and execution continues after the try/catch block. The following example traps two different
exception types:
The following program illustrate multiple catch statements:
Class CatchDemo {