CS3391 QB
CS3391 QB
UNIT I
INTRODUCTION TO OOP AND JAVA
Q.1 Mention some of the separators used in Java programming.
Ans. : Some of the separators that are used in Java are as given below –
Q.3 What is the output of the main method in the given code ?
public static void main(String[] args)
{
screen.write(sum());
}
static int sum()
{
int A=12;
int B=13;
return =A+B;
}
Ans. It will generate error at the statement return = A+B as "Illegal start of
expression".
Q.4 What are the features of Java ?
1) Java is simple to implement.
2) Java is platform independent.
3) It is an object oriented programming language.
4) It is a robust programming language.
5) Java is designed for distributed system.
Following are some differences between the class and the object -
Q.8 What is the difference between static and non static variables?
Ans. A static variable is shared among all instances of class, whereas a non
static variable (also called as instance variable) is specific to a single instance of
that class.
Q.9 What is the difference between structure and class?
Q.14 How would you declare an object of type animal named lion that takes
a weight of 500 and length of 45 as parameters?
Ans.:
public class Animal
{
int weight, length;
Animal(int wt, int len)
{
weight=wt;
length=len;
}
void Show()
{
System.out.println("\n The weight of animal is: "+weight);
System.out.println("\n The length of animal is: "+length);
}
}
class AnimalMain
{
public static void main(String args[]).
{
Animal Lion=new Animal(500,45);
Lion.Show();
}
}
Output
The weight of animal is: 500
The length of animal is: 45
class A
{
public static void main(String args[])
{
System.out.println("Class A");
}
}
class B
{
public static void main(String args[])
{
System.out.println("Class B");
}
}
Output
Step 1: Compile the above program using following command
javac test.java
Step 2: Now execute the above program using
java A
The output will be
Class A
Step 3: If you execute the above code as
java B
The output will be
ClassB
PART – B
PART – C
1.Illustrate a Java application to generate Electricity bill. Create a class with the
following members: Consumer no., consumer name, previous month reading,
current month reading, and type of EB connection (i.e. domestic or commercial).
Compute the bill amount using the following tariff.
If the type of the EB connection is domestic, calculate the amount to be paid as
follows:
i. First 100 units - Rs. 1 per unit
ii. 101-200 units - Rs. 2.50 per unit
iii. 201 -500 units - Rs. 4 per unit
iv. > 501 units - Rs. 6 per unit (15) – (Understand) – BTL2
Q.11 Write a class declaration for the following relationship, assuming that
all classes are public: A Bulldog is a kind of dog, and a Dog is a kind of
Animal.
Ans;
class Animal
{
………….
}
class dog extends Animal
{
………...
}
class Bulldog extends dog
{
…………..
}
Q.12 What is meant by dynamic method dispatch?
Ans. The dynamic method dispatch is run time polymorphism in which a call to
overridden function is resolved at runtime instead of at compile time. Hence is
the name. For example
class Test1
{
public void method1()
{}
}
class Test2 extends Test1
{
public void method1()
{}
}
void main()
{
Test1 obj=new Test2();
obj.method1();
}
Q.19 List any four packages in java and highlight their features.
Ans. :
Q.23 What are the ways of importing the java packages? OR Write the
syntax for importing packages in a Java source file and give an example.
Ans. The import statement can be written at the beginning of the Java program,
using the keyword import. For example -
import java.io.File
or
import java.io.*
Either a class name can be specified explicitly or a * can be used which
indicated that the Java compiler should import the entire package.
PART – B
1. i. Describe in detail about inheritance. (7) (Understand) – BTL2
ii. Write a program for inheriting a class. (6) (Understand) – BTL2
2.i.Illustrate what is super and subclass in Java. (7) (Apply) – BTL3
ii.With an example, illustrate how the objects from sub class are inherited by the
super class. (6) (Understand) – BTL2
3. Examine how to control top level and member level access for the members of
the class. (13) (Remember) – BTL1
4. Illustrate with an example how passing objects as parameters to methods and
returning objects from methods in Java. (13) (Understand) – BTL2
5. Describe in brief about object class and its methods in Javawith suitable
example. (13) (Remember) – BTL1
6.i. Discuss the concept of abstract class. (7) (Understand) – BTL2
ii.Give a program for abstract class with example. (6) (Apply) – BTL3
7.i. Explain briefly on final keyword. (7) (Understand) – BTL2
ii.Explain the concept of abstract class with an example. (6) –(Understand) –
BTL2
8.i.Describe what is meant by interface. (7) (Remember) – BTL1
ii.How is interface declared and implemented in Java. Giveexample. (6)
(Remember) – BTL1
9.i. Differentiate classes with interface with suitable examples. (7)
(Understand) – BTL2
ii. Express a Java program for extending interfaces. (6)
10. Define inner classes. How to access object state using inner classes? Give an
example.(13) (Remember) – BTL1
11.i. Explain with an example what is meant by object cloning? (7)
(Understand) – BTL2
ii. Summarize in detail about inner class with its usefulness. (6)
(Understand) – BTL2
12.Analyse and write a Java program using arrayListclasses and object for the
following operations. (Apply) - BTL3
i.Push (7)
ii.Pop (6)
13.Analyse with an example, how string objects are created. How it can be
modified?(13) (Remember) – BTL1
14.Illustrate String handling class in Java with example. (13) (Understand) –
BTL2
15. Explain in detail about Package with an Example Program. (13) (Understand)
– BTL2
PART – C
1. Illustrate a program to perform string operations using ArrayList. Write
functions for the following
Append - add at end
Insert – add at particular index Search
List all string starts with given letter “a”. (15) (Understand) – BTL2
2. Assess and write an inheritance hierarchy for classes Quadrilateral, Trapezoid,
Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the
hierarchy. Specify the instance variable and methods for each class. The private
instance variables of Quadrilateral should be the x-y coordinate pairs for the four
end points of the quadrilateral.
Write a program that instances objects of your classes and outputs each objects
area (except Quadrilateral) (15) (Apply) – BTL3
3. Consider a class student .Inherit this class in UG Student and PG Student. Also
inherit students into local and non-localstudents. Define five Local UG Students
with a constructor assuming all classes have a constructor. (15) (Apply) – BTL3
4.Express a Java Program to create an abstract class named Shape that contains
two integers and an empty method named print Area (). Provide three classes
named Rectangle, Triangle and Circle such that each one of the classes extends
the class Shape. Each one of the classescontains only the method print Area ()
that prints the area of the given shape. (15) (Understand) – BTL2
UNIT III
EXCEPTION HANDLING AND MULTITHREADING
Q.10 There are three statements in a try block - statement1, statement2 and
statement3. After that there is a catch block to catch the exceptions
occurred in the try block. Assume that exception has occurred in
statement2. Does statement3 get executed or not?
Ans. No. Once a try block throws an exception, remaining statements will not
be executed. Control comes directly to catch block.
Q.12 Does finally block get executed If either try or catch blocks are
returning the control?
Ans. Yes, finally block will be always executed no matter whether try or catch
blocks are returning the control or not.
Q.14 Which class is the super class for all types of errors and exceptions in
java ?
Ans. The java.lang.Throwable is the super class for all types of errors and
exceptions in java.
Q.17 What happens when the statement int value = 25/0 is executed?
Ans. The exception Arithmetic Exception: Divide by zero will occur.
Q.22 What are the three ways by which the thread can enter in waiting
stage?
Ans. :
i) Waiting state: Sometimes one thread has to undergo in waiting state because
another thread starts executing. This can be achieved by using wait() state.
ii) Timed waiting state: There is a provision to keep a particular threading
waiting for some time interval. This allows to execute high prioritized threads
first. After the timing gets over, the thread in waiting state enters in runnable
state. This can be achieved by using the sleep() command.
iii) Blocked state : When a particular thread issues an input/output request then
operating system sends it in blocked state until the I/O operation gets
completed. After
the I/O completion the thread is sent back to the runnable state. This can be
achieved by using suspend() method.
Q.24 Mention two mechanisms for protecting a code block from concurrent
access.
Ans. There are two mechanisms for protecting a code block from concurrent
access -
1. Use of reentrant code
2. Use synchronized block
Q.27 Why do we need run() and start() method both? Can we achieve it
with only run() method?
Ans. We use start() method to start a thread instead of calling run() method
because the run() method is not a regular class method. It should only be called
by the JVM. If we call the run() method directly then it will simply invoke the
caller's thread instead of its own thread. Hence the start() method is called
which schedules the thread with the JVM.
Q.30 When thread is initiated and created, what is its initial stage?
Ans. : A thread is in "Ready" state after it has been created and started. This
state signifies that the thread is ready for execution. From here, it can be in the
running
state.
PART – B
PART – C
UNIT IV
I/O, GENERICS, STRING HANDLING
Q.1What is stream?
Ans. Stream is basically a channel on which the data flow from sender to
receiver.
Q.11 Write a Java code to check if the command line argument is file or
not.
Ans.:
class Test
{
public static void main(String[] args)
{
File obj=new File(args[0]);
If(obj.isFile())
{
System.out.println("This is a file name" + obj.getPath());
}
}
}
Q.13 What is the use of Input Stream Reader and Output Stream Writer?
Ans. The InputStreamReader converts byte into character and
OutputStreamWriter converts the characters written into byte.
Q.14 Give an example for reading data from files using FileInput Stream
Ans;
import java.io.FileInputStream;
public class test
{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream("D:\\input.txt");
int i=0;
while((i=fin.read())!= -1)
{
System.out.print((char)i);
}
fin.close();
}catch(Exception e) { System.out.println(e);}
}
}
Q.18 Can generic be used with inheritance in several ways? What are they?
Ans. Following are the ways by which generic can be used with inheritance -
• Consider a class and a subclass such as Employee and Trainee. There are two
pair classes Pair<Employee> and Pair<Trainee> which do not possess an
inheritance relation. That is, Pair<Trainee> is not a subclass of Pair<Employee>
even if these two classes are related to each other.
• The parameterised raw type can be converted to a raw type.
• The generic classes can extend to implement other generic classes.
PART – C
1. Express multithreading for an sample sequence of strings with a delay of 1000
millisecond for displaying it using Java threads. (15) – (Understand) – BTL2
2. Deduce a Java program to perform the following tasks using three different
threads. Each thread will be responsible for itsown task only. Among these three
threads one will find the average number of the input numbers, one will be
responsible for finding the Maximum number from the input array of numbers,
and one will be responsible for finding the Minimum number from the input array
of numbers. (15) (Apply) – BTL3
3. Express a simple generic class example with two typeparameters. so that we
can define two types of parameters called U & V, separated by ",". (15)
(Understand) – BTL2
4 Assess an example program in Java on how to implement bounded types
(extend superclass) with generics. (15) – (Apply) – BTL3
Q.7 Explain the terms - Stage and scene used in JavaFX application.
Ans. :
1) Stage: Stage is like main frame of the program. It acts as a container for all
the objects of JavaFX application. The most commonly used stage is a
PrimaryStage.
2) Scene: The Javafx.scene.Scene class provides the methods to deal with the
scene object. It contains the nodes or all the contents of Scene graph.
Q.8 Explain the terms - Scene graph and node used in JavaFX.
Ans. :
1) The scene graph is a collection of various nodes.
2) The node is an element that can be visualized on the screen. The node can be
button, textbox, radio button and so on.
1.i. Describe in detail about working with 2D shapes in Java. (7) (Understand) –
BTL2
ii. Identify a Java program to illustrate Mouse Events. (6) (Apply) – BTL3
2.i..Describe in detail about swing Components. (7) ii.Describe the types of
layout management. (6) (Understand) – BTL2
3. Summarize in detail about graphics programming. (13) (Understand) –
BTL2
4.I.Explain how an application can respond to events in Java? Write the steps and
the example. (7) (Understand) – BTL2
ii. Explain the adapter class using example. (6) (Understand) – BTL2
5. What is meant by event handling? Analyze and write a simple calculator using
mouse events that restrict only addition, subtraction, multiplication and division.
(13) (Remember) – BTL1
6. Tabulate the controller design pattern and components ofswing briefly.
(13) (Apply) – BTL3
7.i.Illustrate what is layout management? State the various types of layout
supported by Java? Which layout is default one? (7) (Understand) – BTL2
ii.Examine the basic of event handling. (6)
8. Explain with an example program and discuss in detail about Mouse listener
and Mouse Motion Listener. (13) (Understand) – BTL2
9.i.Illustrate the methods available in graphics for COLOR. (7) (Understand) –
BTL2
ii.Examine the methods available to draw shapes. (6)
10 i. Explain on AWT Event Hierarchy (7) (Understand) – BTL2
ii. Explain about Semantic and Low-Level Events (6) (Understand) –
BTL2
11. List the characteristics of Model View Design (MVC) patterns. Explain the
advantage of MVC and methods MVC. (13) (Remember) – BTL1
12.i.List the types of adjustment events in scrollbar. (7) (Remember) – BTL1
ii.Explain and write a program to demonstrate the usage of Scroll bar. (6)
(Understand)
13 Examine the following in detail (Understand) – BTL2
i. Handling a TextField. (7)
ii. Using a TextArea. (6)
PART – C
1. Illustrate a Java program to implement the following (Understand) – BTL2
Create four check boxes. The initial state of the first box should be in checked
state. The status of each check boxshould be displayed. When we change the
state of a check box, the status should be displayed and updated. (15)
2. Express a Java program to display the following picture as (15)
(Understand) – BTL2
Output.