0% found this document useful (0 votes)
16 views

CS3391 QB

Uploaded by

santhikala
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CS3391 QB

Uploaded by

santhikala
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CS3391 - OBJECT ORIENTED PROGRAMMING

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.2 How dynamic initialization of variables is achieved in java ?


Ans. The variables in Java are allowed to get initialized at run time. Such. type
of initialization is called dynamic initialization.
For example
double a=5.0;
double b=20.0
double c=Math.sqrt((a+b));
Here the expression is evaluated at run time and its square root value is
calculated and then the variable c is initialized dynamically.

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.

Q.5 Define objects and classes in Java.


Ans. An object is an instance of a class. The objects represent the real world
entity. The objects are used to provide a practical basis for the real world. Each
class is a collection of data and the functions that manipulate the data. The data
components of class are called data fields and the function components of the
class are called member functions or methods.

Q.6 Why are classes important in OO technology?


Ans. Classes bind together the relative data and methods in a cohesive unit. Due
to this arrangement, only certain methods are allowed to access the
corresponding data. Secondly, if any modification is needed, then the class can
be viewed as one module and the changes made in one class does not spoil rest
of the code. Moreover, finding error from such a source code becomes simple.
Hence use of class is very important thing in OO technology.

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.10 Define encapsulation.


Ans. Encapsulation means binding of data and method together in a single
entity called class.

Q.11 What is an abstract class?


Ans. When apply inheritance, the class becomes more and more specific as we
move down. Sometimes a situation may occur that the superclass becomes more
general and less specific. Such a class lists out only common features of other
classes. Such a super class is called as abstract class.

Q.12 Define class. Give example.


Ans. Each class is a collection of data and the functions that manipulate the
data. The data components of class are called data fields and the function
components of the class are called member functions or methods.
For example
class Customer
{
int ID;
String Name; //Data Field
Customer() //Constructor
{}
double withdraw_money() //method {...}
}
Q.13 What are the benefits of encapsulation? Should abstractions be user
centric or developer- centric ?
Ans. Due to encapsulation the corresponding data and the methods get bound
together by means of class. The data inside the class is accessible by the
function in the same class. It is normally not accessible from outside
component. Thus the unwanted access to the data can be protected.
The abstraction should be user centric. While developing the system using the
O0 principles it is important to focus the user and not the developer.

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

Q.15 What is meant by private access specifier?


Ans. The private access specifier allows classes, methods and data fields
accessible only from within the own class. The functions outside the class
cannot access the data members or the member functions.

Q.16 What do you mean by instance variable?


Ans. : An Object is an instance of a class. The variables that the object contains
are called instance variables. For example consider a class Student
class Student
{
int RollNo;
char name[10];}
Student S; //Creation of object of type Student class.
S= new Student();
If we create an object of the class Student say S, then using this object the
variables RollNo and name can be accessed. These variables that belong to
object S are then called as instance variables. Thus the Instance variables are
any variables, without "static" field modifier, that are defined within the class
body.

Q.17 Define constructor.


Ans. Constructor is a specialized method used for initializing the objects. The
name of this method and the name of the class must be the same. The
constructor is invoked whenever an object of its associated class is created.

Q.18 What is Static in Java ?


Ans. In java, static is a member of a class that is not associated with an instance
of a class. If there is a need for a variable to be common to all the objects of a
single java class, then the static keyword should be used in the variable
declaration.

Q.19 What is the difference between a constructor and a method?


Ans. :

Q.20 What are key characteristics of objects?


Ans. :
1.Object is an instance of a class.
2.Objects are runtime entities.
3. Using object of a class the member variable and member functions of a class
can be accesses.
Q.21 What is meant by parameter passing constructors? Give example.
Ans. The constructor methods to which the parameter are passed are called
parameter passing constructors
Example -
Rectangle(int h,int w)
{
height=h;
weight=w;
}

Q.22 Enumerate two situations in which static methods are used.


Ans. :
1) The situation in which object of belonging class is not created and we want to
use the method of that class, then the method must be static.
2) For calling another static method, one such static method is used.
3) For accessing static data the static method must be used.

Q.23 List any four Java Doc comments.


Ans. :

Q.24 What is the need for javadoc multiline comments?


Ans. Javadoc multiline comments can be used to document all java source code.
Comments follow a standard format consisting of a description followed by
block tags. The first sentence of the description should be clear and concise as it
is used in the summary of the API item.

Q.25 Define access specifier.


Ans. Access Specifier is used to set the accessibility of class members. There
are three access specifiers in Java - (1) Public (2) Private (3) Protected.
Q.26 What is Javadoc ?
Ans. The Javadoc is a standard way to comment the java code. It has a special
format of commenting the java code.
Q.27 Can Java source file be saved using a name other than the class name.
Justify.
Ans. Yes, we can save the java source file using a name other than the class
name. But we should compile the program with file name and should run the
program with the class name in which the main method exist. And there must
not be any public class defined inside this java source file. For example,
consider following Java code which is saved using the file name test.java

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

1.I. Explain OOPS and its features. (7) –(Understand) – BTL2


ii.Summarize about the usage of constructor with an example using Java. (6) –
Understand – BTL2
2 i. What is class? How do you define a class in Java? (7) (Understand) –
BTL2
ii.Examine the use of inheritance and class hierarchy. (6) (Analyze) – BTL4
3 i. Define polymorphism. (7) (Remember) – BTL1
Ii Describe variables and operators in Java. (6) (Understand) – BTL2
4. Define and explain the control flow statements in Java withsuitable examples.
(13) (Remember) – BTL1
5. What is meant by constructor? Discuss the types of constructor with
example. (13) (Understand) – BTL2
6 i.Analyse and Develop a simple Java program to sort the (7)
given numbers in increasing order.(Apply) – BTL3
ii.Write a Java program to reverse the given number. (6) (Apply) – BTL3
7. i. Classify the characteristics of Java. (6) (Understand) – BTL2
ii.Illustrate the working principles of Java Virtual Machine. (4) (Understand) –
BTL2
iii.Show with an example the structure of Java Program (3) (Understand) –
BTL2
8. i.Summarize about access specifier in Java. (7) (Understand) – BTL2
ii.Describe the term static fields and methods and explain itstypes with example.
(Understand) – BTL2 (6)
9. i) Define Arrays. What is array sorting and explain with an example (7)
(Understand) – BTL2
ii) Tabulate and explain documentation comments in Java. (6) (Understand) –
BTL2
10. Illustrate what is meant by package? How its types are created and
implemented in Java. (13) (Understand) – BTL2
11. Write the techniques to design classes in Java usingJavaDoc. (13) Apply –
BTL3
12. Explain with example passing objects as parameters to methods and
returning objects from methods in Java. (13) - (Understand) – BTL2
13. Explain packages in Java with example. (13) – (Understand) – BTL2
14. Interpret with an example what is method overloading and method
overriding. (13) (Understand) – BTL2

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

2. Explain a Java program to find a smallest number in the given array by


creating one dimensional array and two dimensional array using new operator.
(15) – (Understand) – BTL2
3. Explain class hierarchy and explain its types with suitable examples. (15) –
(Understand) – BTL2
4. Express a Java application with Employee class with Emp_name, Emp_id,
Address, Mail_id, Mobile_no as members. Inherit the classes, Programmer,
Assistant Professor, Associate Professor and Professor from employee class. Add
Basic Pay (BP) as the member of all the inherited classes with 97% of BP as DA,
10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club fund. Generate pay
slips for the employees with their gross and net salary. (15) (Understand) –
BTL2
UNIT II INHERITANCE, PACKAGES AND INTERFACES

Q.1 Define the term inheritance.


Ans. Inheritance is a mechanism in which the derived class borrows the
properties of the base class.

Q.2 Explain the use of extend keyword with suitable example.


Ans;
• The extend keyword is used in inheritance.
• When the child class is derived from its parent class then the keyword extends
is used.

Q.3 What is the difference between superclass and subclass?


Ans. :
• The superclass is a parent class or base class from which another class can be
derived.
• The subclass is always a derived class which inherits the properties of the
base class or superclass.

The superclass is normally a generalized class whereas the subclass is normally


for specific purpose.

Q.4 Enlist various forms of inheritance.


Ans. Various forms of inheritance are -
1. Single level inheritance
2. Multiple inheritance
3. Multi-level inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

Q.5 What is the use of super keyword ?


Ans. The super class is used to access immediate parent class from the subclass.
It is used to
1. access parent's variable,
2. access parent's method
3. access patent's constructor invocation
Q.6 Differentiate between inheritance and polymorphism.
Ans. :

Q.7 Distinguish between static and dynamic binding.


Ans. Static binding is a type of binding in which the function call is resolved at
compile time. The dynamic binding is a type of binding in which the function
call is resolved at run time. The static binding is called the early binding and the
dynamic binding is called late binding.

Q.8 What is the purpose of 'final' keyword ?


Ans. The keyword 'final' is used to avoid further modification of a variable,
method or a class. For instance if a variable is declared as final then it can not
be modified further, similarly, is a method is declared as final then the method
overriding is avoided.

Q.9 Define inheritance hierarchy.


Ans. The inheritance hierarchy represents the collection of classes the inherit
from their base classes and thereby make use of the code present in the base
class. The data reusability can be achieved by inheritance hierarchy. For
example
Q.10 How to prevent inheritance?
Ans. If we declare particular class as final then no class can be derived from it.
For example
final class Test
{
………..
………
……….
}
class Test1 extends Test
{
………..
………
……….
}
The above code will produce an error" cannot inherit from final Test".
Thus the use of keyword final for the class prevents the inheritance.

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.13 Can an abstract class be final? Why?


Ans. The abstract class cannot be final because once we declared the abstract
base class with the keyword final then it can not be inherited.

Q.14 Can an abstract class in Java be instantiated? Give the reason.


Ans. The abstract class can not be instatiated (i.e. we can not create the object of
this class using new operator) because the abstract class is very much general
and less specific. It does nothing and simply lists out only common features of
other classes.

Q.15 Why is multiple inheritance using classes a disadvantage in Java?


Ans. In multiple inheritance, the child class is derived from two or more parent
classes. It can lead to ambiguity when two base classes implement a method
with the same name. Q.16 State the condition for method overriding in Java
Ans. Method overriding occurs only when the name of the two methods(method
in super class and method in subclass) are same and their type signature is same.

Q.17 What is package?


Ans.: Package represent a collection of classes, methods and interfaces. The
name of the package must be written as the first statement in the Java source
program. The syntax of specifying the package in the Java program is
package name_of_package
Due to package the systematic arrangement of classes is possible. All standard
classes in Java are stored in named packages.

Q.18 Mention the necessity for import statement.


Ans. The import statement is used to refer the classes and methods that are
present in particular package. This statement is written at the beginning of any
Java program. For example -
import java.io.*
This statement allows to use the useful functionalities for performing input and
output operations.

Q.19 List any four packages in java and highlight their features.
Ans. :

Q.20 What is API package?


Ans.: • The API packages are application programming interface packages used
by java.
• These packages include important classes and interfaces which are used by
the programmer while developing the java applications.
• For example java.io, java.util, java.net and so on are the commonly used API.

Q.21 Explain any three uses of package.


Ans. :
1.The classes defined in the packages of other program can be easily reused.
2. Two classes from two different packages can have the same name. By using
the package name the particular class can be referred.
3. Packages provide the complete separation between the two phases- design
and coding.
Q.22 Explain the term CLASSPATH.
Ans. The packages are nothing but the directories. For locating the specified
package the java run time system makes use of current working directory as its
starting point. This directory path is called CLASSPATH.

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.

Q.24 In Java what is the use of interface?


OR What is interface mention its use.
Ans. In java, the interface is used to specify the behaviour of a group of classes.
Using interfaces the concept of multiple inheritance can be achieved.

Q.25 Define Interface and write the syntax of the Interface.


Ans. The interface can be defined using following syntax access_modifier
interface name_of_interface
{
return_type method_name1(parameter1,parameter2,...parametern);
…………
return_type method_name1(parameter1,parameter2,...parametern);
type static final variable_name=value;
………...
}
The interface is a collection of various methods that can be used by the class
that is attached to the interface. The interface name must be preceded by the
keyword interface.

Q.26 What modifiers may be used with an interface declaration ?


Ans. : An interface may be declared as public or abstract.

Q.27 Why the variables in interface static and final ?


Ans. The members of interface are static and final because -
1) The reason for being static - The members of interface belong to interface
only and not object.
2) The reason for being final - Any implementation can change value of fields if
they are not defined as final. Then these members would become part of the
implementation. An interface is pure specification without any implementation.

Q.28 What is the purpose of nested interface?


Ans. The nested interfaces are used to group related interfaces so that they can
be easy to maintain. The nested interface must be referred by the outer interface
or class. It can't be accessed directly.
Q.29 What are the properties of nested interface?
Ans;
1) Nested interfaces are static by default. You don't have to mark them static
explicitly.
2) Nested interfaces declared inside class can take any access modifier.
3) Nested interface declared inside interface is public implicitly.

Q.30 If a class B is derived from class A and extends the interface


myinterface, then how will you write this statement for class B definition?
class B extends class A implements myinterface
{
....//body of class B
}

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.1 What is an exception? Give example.


Ans: Exception is a mechanism which is used for handling unusual situation
that may occur in the program. For example -
ArithmeticException: This exception is used to handle arithmetic exceptions
such as divide by zero,
IOException: This exception occurs when an illegal input/output operation is
performed.

Q.2 What will happen if an exception is not caught?


Ans. An uncaught exception results in invoking of the uncaughtException()
method. As a result eventually the program will terminate in which it is thrown.

Q.3 What is the benefit of exception handling?


Ans. When calling method encounters some error then the exception can be
thrown. This avoids crashing of the entire application abruptly.

Q.4 What is the difference between error and exception in java?


Ans. Errors are mainly caused by the environment in which an application is
running. For example, OutOfMemoryError happens there is shortage of
memory. Where as exceptions are mainly caused by the application itself. For
example, NullPointerException occurs when an application tries to access null
object.

Q.5 What is compile time and run time error?


Ans;
1.The errors that are detected by the Java compiler during the compile time are
called compiler time errors.
2. The runtime errors are basically the logically errors that get caused due to
wrong logic.

Q.6 What is the use of try, catch keywords?


Ans. : try - A block of source code that is to be monitored for the exception.
catch - The catch block handles the specific type of exception along with the try
block.

Q.7 What is the difference between throw and throws?


Ans. :
1. The throw keyword is used to explicitly throw an exception.
2. The throws keyword is used to declare an exception.

Q.8 What is ArrayIndexOutOfBoundsException?


Ans. When we use an array index which is beyond the range of index then
ArrayIndexOutOfBoundsException occurs.

Q.9 What is the need of multiple catch?


Ans.:
• There may be the situations in which different exceptions may get raised by a
single try block statements and depending upon the type of exception thrown it
must be caught.
• To handle such situation multiple catch blocks may exist for the single try
block
statements.

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.11 Explain the types of exceptions.


Ans. There are two types of exceptions:
1. Checked Exception: These types of exceptions need to be handled explicitly
by the code itself either by using the try-catch block or by using throws. These
exceptions are extended from the java.lang.Exception class.
For example: IOException which should be handled using the try-catch block.
2. Unchecked Exception: These type of exceptions need not be handled
explicitly. The Java Virtual Machine handles these type of exceptions. These
exceptions are extended from java.lang.RuntimeException class.
For example: ArrayIndexOutOfBounds, NullPointerException,
RunTimeException.

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.13 Can we have empty catch block?


Ans. Yes we can have empty catch block, but it is an indication of poor
programming practice. We should never have empty catch block because if the
exception is caught by that block, we will have no information about the
exception and it will create problem while debugging the code.

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.15 What is runtime exceptions?


Ans. RuntimeException is the superclass of those exceptions that can be thrown
during the normal operation of the Java Virtual Machine. Runtime Exception is
the parent class in all exceptions of the Java.

Q.16 What is exception handling?


Ans. Exception handling is a mechanism in which the statements that are likely
to cause an exception are enclosed within try block. As soon as exception
occurs it is handled using catch block.
Thus exception handling is a mechanism that prevents the program from
crashing when some unusual situation occurs.

Q.17 What happens when the statement int value = 25/0 is executed?
Ans. The exception Arithmetic Exception: Divide by zero will occur.

Q.18 What do you mean by threads in Java ?


Ans. Thread is tiny program running continuously. It is a light weight process in
Java..
Q.19 Give the difference between process and thread.
Ans.

Q.20 What are different stages in thread?


Ans. Various stages in thread are
1. New state
2. Time waiting state
3.Runnable state
4. Waiting state
5.Blocked state
6. Terminated state

Q.21 What do you mean by synchronization ?


Ans. When two or more threads need to access shared memory, then there is
some way to ensure that the access to the resource will be by only one thread at
a time. The process of ensuring one access at a time by one thread is called
synchronization.

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.23 What is multithreading?


Ans. Multithreading is an environment in which multiple threads are created
and they can execute simultaneously. The multiple threads can be created either
by extending the thread class or by implementing the runnable interface.

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.25 What is meant by notify methods in multithreading?


Ans. The notify() method is used for inter thread communication. If a particular
thread is in the sleep mode then that thread can be resumed by using the notify
call.

Q.26 What are the two ways of creating a thread?


Ans. Thread can be created using
1. Thread class 2. runnable interface.
2.

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.28 What is the need for thread?


Ans. In Java threads are used to handle multiple tasks together. This kind of
programming is called concurrent programming.

Q.29 Name any four thread constructor.


Ans. :
1. Thread()
2. Thread(String name).
3. Thread(Runnable target)
4. Thread (Runnable target, String name)

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.

Q.31 "Thread is a lightweight process" - Comment


Ans.
Threads do not require separate address for its execution. It runs in the address
space of the process to which it belongs to. Hence thread is a lightweight
process.

Q.32 Sketch the life cycle of thread


Ans.

PART – B

1. Discuss in detail about exception handling constructs and write a program to


illustrate Divide by zero exception. (13) (Understand) –BTL2
2. Describe the following concepts with example. (Remember) – BTL1
i. Try-catch-throw paradigm. ii.Exception specification. (7)(6)
3. Describe about the syntax of catch and re-throw anexception with an
example. (Remember) – BTL1 (13)
4. (i) Point out the importance of exception hierarchy. (7) (Understand) –
BTL2
(ii)Explain on stack trace elements give example (6)
5. Tabulate any five classes to support exception handling in Java with an
example for each. (13) (Remember) – BTL1
6.i. Summarize what is finally class. How to catch exception? Write an example.
(7) (Understand) – BTL2
ii. Give short notes on Java built in exceptions (6)
7.Explain the following in detail with example program (Understand) – BTL2
i.Checked Exception (7)
ii.Unchecked exception (6)
8.i.Classify the errors and exception in Java. (7) (Apply) – BTL3
ii.Illustrate about multiple catching exceptions with example. (6)
(Understand) – BTL2
9. Summarize the following with example program (Apply) – BTL3
i.Arithmetic exception (5)
ii.Arrayoutofbound exception (4)
ii.Stringindexoutofbound exception (4)
10.i. Express a program to read and count the characters from files. (7)
(Understand) – BTL2
ii.Illustrate a Java program to transfer the content of one file toanother file. (6)
11. Discuss briefly about the features (Understand) – BTL2
i. Byte streams input/output (7)
ii. Character streams input/output (6)
12. Explain the following with example (Understand) – BTL2
i. Reading console input (7)
ii. Writing console output. (6)
13.i.Identify a Java program to read characters from the console. (7) (Remember)
– BTL1
ii.Identify a Java program to read strings from the console. (6)
14.Illustrate in brief about (Apply) – BTL3
i. Reading from a file. (7)
ii. Writing in a file. (6)
15. Explain in detail about Thread and its types. (13) (Understand) – BTL2

PART – C

1. Implement a Java program for user defined exceptionhandling. (15) (Apply) –


BTL3
2.i.Custom exception has been created in the code given below.Correct and
evaluate the code (15) (Apply) – BTL3
Class myexception extends Exception
{
Myexception(string s)
{
super(s)
}
}
Class excep
{
Public static void main (String args [])
{‘ if (args [0] == “Hello”) System.out.println(“String is right”);
else try
{
Throw new myexception(“Invalid string”);
}
Catch(myexception ex)
{
System.out.println(ex.gemessage());
}
}
}
ii.The program calculates sum of two numbers inputted as command-line
arguments.When will it give an exception? Class execp
{
Public static void main( String []args)
{
try{
int n= Integer.parseInt(arg[0]); int n1=Integer.parseInt(arg[1]); int n2=n+n1;
System.out.println(“Sum is:” +n2);
}
Catch(NumberFormatException ex)
{
System.out.println(ex);
}
}
}
3.Illustrate the Java program to concatenate the two files and produce the output
in the third file. (15) (Understand) – BTL2
4.Deduce a Java program that reads a file name from the user,displays
information about whether the file exists, whether thefile is readable, or writable,
the type of file and the length of the file in bytes.(15) (Apply) – BTL3

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.2 What is input stream and output stream?


Ans. An input object that reads the stream of data from a file is called input
stream and the output object that writes the stream of data to a file is called
output stream.

Q.3 What is byte stream? Enlist its super classes.


Ans. The byte stream is used for inputting or outputting the bytes. The
InputStream and OutputStream are the superclass of byte stream.

Q.4 What is character stream? Enlist its super classes.


Ans. The character stream is used for inputting or outputting the characters. The
Reader and Writer are the superclass of character stream.
Q.5 List the byte stream classes.
Ans. The Byte Stream classes are -
1. FileInputStream
3. FilterInputStream
5. FileOutputStream
7. FilterOutputStream
2. PipedInputStream
4. ByteArrayInputStream
6. PipedOutputStream
8. ByteArrayOutputStream
Q.6 What is the purpose of BufferedInputStream and
BufferedOutputStream classes?
• These are efficient classes used for speedy read and write operations.
• We can specify Buffer size while using these classes.

Q.7 Give an example on stream.


• There are two types of streams - Byte stream and Character stream.
• Stream classes for byte stream : FileInputStream, PipedInputStream,
BufferedInputStream, FileOutputStream, PipedOutputStream and so on.
• Stream classes for character stream: FileReader, PipeReader, BufferedReader,
FileWriter, PipeWriter, Buffered Writer and so on.

Q.8 What is absolute file name?


Ans. The filename that can be specified with the complete path name and drive
letter is called absolute file name.

Q.9 What is relative file name?


Ans. The file name which is specified as a path relative to the current directory
is called relative file name. For example new File("myprogram.html");

Q.10 What is the use of seek method?


Ans. : The seek() allows you to specify the index from where the read and write
operations will start. The syntax is
void seek(long position);

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.12 Enlist the two common constructors of FileInputStream.


Ans. The common constructors of FileInputStream are
FileInputStream(String filename);
FileInputStream(File fileobject);

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.15 Why generic programming is required?


OR What is the need for generic programming?
Ans. Following some reasons for the need of generic programming -
1. It saves the programmers burden of creating separate methods for handling
data belonging to different data types.
2. It allows the code reusability.
3. Compact code can be created.

Q.16 List out motivation needed in generic programming.


Ans;
1. Suppose we want to create a stack and if we create a stack of integers then it
will store only the integer elements, if we try to push any string or any double
type element then a compile time error will occur. If we want to push any string
then we need to create a separate stack, separate class and separate methods for
handling the String elements. Same is true for the elements of any other data
type. This results in complex code building. To avoid such complexity, a
concept of generic programming is introduced.
2. It saves the programmers burden of creating separate methods for handling data
belonging to different data types.
3.It allows the code reusability..
3. Compact code can be created.

Q.17 With an example define a generic class. Or Describe generic classes.


Ans. A generic class contains one or more variables of generic data type.
Following is a simple example which shows how to define the generic class.
public class Test<T>
{
public Test(){val=null;}
public Test(T val)
{
this.val=val;
}
public getVal()
{
return val;
}
public setVal()
{
val=newValue;
}
private T val; //variable defined as of generic type
}

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.

Q.19 List any two advantages of type parameters.


Ans. :
1.Due to the use of type parameter it saves the programmers burden of creating
separate methods for handling data belonging to different data types.
2. Due to type parameter the early error detection at compile time occurs. This
avoids crashing of the code(due to type incompatibility) at run time.

Q.20 State any two challenges of generic programming in virtual machine.


Ans. :
i) The virtual machine does not work with generic classes or generic methods.
Instead it makes uses raw types in which the raw types are replaced with
ordinary java types. Each type variable is replaced with its bound or with object,
if it is not bounded. This technique is called type erasure.
ii) In order to handle the type erasure methods the compiler has to generate
bridge methods in corresponding class.
PART – B

1. Describe in detail about multithread programming with (13) example.


(Understand) – BTL2
2.I.Differentiate multithreading and multitasking. (7) (Remember) – BTL1
ii.Describe the properties of thread in detail. (6)
3. Summarize the two types of thread implementation
supported by Java .Give example. (13) (Understand) – BTL2
4.i.Illustrate the concept of synchronization in thread. (7) (Understand) –
BTL2
ii.Write a Java code for reader writers problem. (6)
5. iDescribe how to implement runnable interface for creatingand starting
threads. (7) – (Understand) – BTL2
ii.Define threads. Describe in detail about thread life cycle. (6) (Remember)
– BTL1
6.i.Explain what is inter-thread communication? List out the methods used for it.
(7) – (Understand) – BTL2
ii. Explain inter-thread communication using producerconsumer problem. (6)
7. Summarize the following (Understand) – BTL2
i. Thread priorities (7)
ii. Deamon thread (6)
8. Explain the following (Understand) – BTL2
i) States of a thread with a neat diagram (7)
ii) Explain how threads are created in Java (6)
9.i.Illustrate the motivations of generic programming. (7) ii.Develop a program
to show generic class and methods withexample. (6) (Understand) – BTL2
10.i.Describe in detail about bounded types with suitable example. (7)
(Understand) – BTL2
ii.List the inheritance rules for generic types with example. (6)
11.i.Give the limitations of generic programming. (7) (Understand) – BTL2
ii.Explain any two restriction of generic programming in detail with suitable
example. (6)
12 Describe the following (Understand) – BTL2
i. Generic class (7)
ii. Generic method (6)
13.Illustrate generic code and the virtual machine with suitableexample. (13)
(Understand) – BTL2
14. Summarize thread group. How to implement the thread group. Explain it with
example. (13) – (Apply) – BTL3

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

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS

Q.1 What is JavaFX ?


Ans. :
• JavaFX is a set of graphics and media packages that enables developers to
design, create, test, debug and deploy rich client applications that operate
consistently across different platforms.
JavaFX makes it easier to create desktop applications and games in Java.

Q.2 What are various features of JavaFX ?


Ans. :
1) JavaFX library is written in java. Hence developer find it easy to learn and
write the applications in JAVAFX.
2) JAVAFX library creates UI controls using which any GUI based application
can be developed.
3) It provides the classes for 2D and 3D graphics.
4) JavaFX contains a set of ready-to-use chart components, so you don't have to
code that from scratch every time you need a basic chart.

Q.3 Is Javafx Open Source ?


Ans. Yes.

Q.4 What are the main components of JavaFX application?


Ans.
Every JAVAFX program is divided into three main components - Stage, Scene
and Node and Scene graph.
Q.5 Which method is used to launch JavaFX application?
Ans. The launch() method is used to launch JavaFX application.
Ans. The start() method is used in which we write the code for JavaFX
application.

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.

Q.9 What is the use of pane in JavaFX application?


Ans. :
Pane is a container class using which the UI components can be placed at any
desired location with any desired size.
Generally you place a node inside a pane and then place pane into a scene.
Actually node is any visual component such as UI controls, shapes, or a image
view.

Q.10 What is property binding in JavaFX ?


Ans.
Property binding is a technique that enables target object to bind with the source
object. Due to this mechanism, when the value in source object changes then the
target property also changes automatically.
Q.11 What is Event?
Ans. Event means any activity that interrupts the current ongoing activity.
For example: When user clicks button then it generates an event. To respond to
button click we need to write the code to process the button clicking action.

Q.12 What is event source object?


Ans. The object that generates the event is called event source object. For
example - If the event gets generated on clicking the button, then button is the
event source object.

Q.13 Explain the terms - event handler and event listener.


Ans. Event Handler: The event handling code written to process the generated
event is called event handler.
Event Listener: The task of handling an event is carried out by event listener.
When an event occurs, first of all an event object of the appropriate type is
created. This object is then passed to a Listener.
Q.14 Explain the event methods in JavaFX that are associated with
keyboard.
Ans.

Q.15 Explain any two UI controls used in JavaFX application.


Ans :
Q.16 What is layoutpane?
Ans. :
• The arrangement of various components(nodes) in a scene within the container
is called layout of the container.
• For using the layout we must import the package javafx.scene.layout. The
class named Pane is the base class for all the layouts in JavaFX.

Q.17 Enlist various layout panes used in any JavaFX application?


Ans.

Q.18 Explain the terms - Menubar and Menu item.


Ans. :
• MenuBar is just like a navigation bar with menus on it. The menubar is
located at the top of the screen.
• A MenuItem is a basic item that goes on a menu.
PART – B

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.

3. Explain a Java program for event handling using


actionlistener interface (15) (Understand) – BTL2
4. Recommend a Java swing with one button and adding it on the JFrame object
inside the main() method. (15) (Apply) – BTL3

You might also like