0% found this document useful (0 votes)
84 views16 pages

Viva Questions All Chapter OOP JAVA (2024)

Uploaded by

royaldecor051
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views16 pages

Viva Questions All Chapter OOP JAVA (2024)

Uploaded by

royaldecor051
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Skill base Lab course: OOP with Java Compiled by : Prof.

Deepak Gaikar

Chapter Wise Viva Question (COMP)


Chapter 1: Introduction to OOP
 OOPS different concept (8 concept)
 What is Real world object with example
 Explain Class & Object
 Data encapsulation /data hiding /polymorphism /data abstraction /inheritance /message
passing

Introduction of java
 Feature of Java (Refer All Basic Future)
 What Is Bytecode,Compiler, Interpreter
 Explain JVM
 Explain Platform Independent
 Different JDK Tools in Java
 Built In Packages in Java (java.lang ,java.util, java.io,java.awt,java.applet)
 Control Statement (if-else ,switch ,for ,while, break, continue)

Chapter 2: Classes,Objects,Packages & Input/Output


 Definition: Classes & Object With Example
 How To Create Object In Java
 How Many Object Created using Single Class
 What Is Local & Reference Variable
 Why Using Static Keyword With Main
 Explain Static Method
 Call By Value And Call By Ref
 Recursive Method.
 Concepts Of Constructor, Type Of Constructor Type.
 Java Garbage Collector.

Chapter 3: Array,String and Vector


 Difference Between Array & Vector
 1 D & 2 D Array Syntax
 Initial Capacity of Vector Method
 String class and Different String method
 StringBuffer class and Different StringBuffer class methods

Chapter 4: Inheritance and Interface


 Types of inheritance
 How To Create Object In Inheritance(Using Base Class Or Derived Class)
 Concepts of Public, Private ,Protected and default access specifier
 If Base Class Data Private : How To Access Derived Class Base Class Private Data
 Use of Method Overloading & Method Overriding (Both Polymorphism, Concept)
 Use Of Super Keyword (How To Access Variable Method Constructor Using Super
Keyword)
 Why Using Final Keyword In Java (Final With Class, Final With Variable, Final With
Method)
 Abstract Class & Method /Different Between Abstact Method And Method Overriding
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

 What Is Multiple Inheritance /Why Java Does Not Used Multiple Inheritance
 Why Using Interface /How Many Interface Can Be Implement In Derived Class
 Syntax of Interface Declaration & Implementation In Derived Class

Chapter 5: Exception Handling & Multithreading

 Type of Error In Program


 Different Type of Exception Handling
 Try Catch Finally Keyword
 Different Between Throw & Throws
 why java is multithreaded programming ,
 thread, life cycle of thread ,diff thread method

Package:

 Why using Packages in Java


 Built In Packages in Java (java.lang ,java.util, java.io,java.awt,java.applet)
 How to Declared User Defined Packages

Chapter 6: GUI programming in JAVA

 Different Between Applet & Application Program


 Different Stages of Applet Life Cycle
 Paint Method
 Why Using HTML Code
 Why Using AWT Package In Applet
 How To Executed Applet Program
 Applet HTML Tag
 Parameter Passing In Applet
 Swing in java

Few Viva Questions With Solution In Java

Q) How Application is differ from Applet?


Applications:
 Applications are Stand Alone and the doesn’t need web-browser.
 Execution of Applications starts with main ().
Applets:
 Needs no explicit installation on local machine. Can be transferred through
Internet on to the local machine and may run as part of web-browser.
 Execution Applets starts with init () method and it must run within a GUI, it may
be AWT / Swing.

Q) Why JAVA is known as securable language?


There are two reasons behind this. The first one is, as JAVA runs in JVM, which is a
virtual machine, and it converts source code to byte code, which is not understandable
for humans, so its safe. The second reason is JAVA does not support the Pointer
concept, hence we can say that JAVA is a securable language.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q) What is the necessity of Interface?


As java is a Partially Object Oriented language, and it does not support the multiple
inheritance concepts, it creates problem for the developers. To over come this
situation JAVA provides Interface concept for multiple inheritance

Q) What is Abstraction?

An essential element of object-oriented programming is abstraction. Abstraction is the


process to hide the complexity. A powerful way to manage abstraction is through the
use of hierarchical classifications. This allows programmer to understand the
semantics of complex system.

Q) What is the difference between interface and abstract class?

Difference between interface and abstract:


All the methods declared inside interface should be abstract, and there is no need to use the
key word “abstract” for those method, but in case of abstract class at least one method should
be abstract, most importantly u have to use the “abstract” key word for that method, besides
that it may contain concrete methods. Abstract class must be extended abstract () methods
generally contain only declaration part without definition part.

Q) What is Polymorphism?

The meaning of Polymorphism is something like one name many forms. Polymorphism
enables one entity to be used as general category for different types of actions. The specific
action is determined by the exact nature of the situation. The concept of polymorphism can be
explained as “one interface, multiple methods”.

Q) What is Inheritance?

Inheritance is the process of basing one class on another. In this process, a base class provides
methods, properties, and other members to a derived class. The advantage of inheritance is
that you can write and maintain code once it is in the base class and reuse it over and over in
the derived classes. But the main drawback of Java is it doesn’t support multiple Inheritances;
hence to overcome this draw back Java provides us the Interface concept.

Q) What is Object Oriented Programming Language?

The language, which obeys all the basic principles of Object Oriented Programming, which are
Encapsulation, Inheritance and Polymorphism, is generally known as Object Oriented
Programming Language. Languages like c++ and java are some well-known Object Oriented
Programming Languages.

Q) What is Encapsulation?

Encapsulation is a process of binding or wrapping the data and the codes that operates on the
data into a single entity. This keeps the data safe from outside interface and misuse.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q) What is Class and Object?

A class defines the abstract characteristics of a thing (object), including the thing’s
characteristics, like its attributes or properties and the things it can do .The code for a class
should be relatively self-contained. Collectively, the properties and methods defined by a class
are called members. Object a particular instance of a class. Object holds the characteristics of
the Class. The set of values of the attributes of a particular object is called its state.

Q) Is JAVA a completely Object Oriented Programming Language?


The answer to this question is certainly no. The reason behind this is JAVA does not obey the
OOPs concept purely. Certain concepts like Operator over loading and Multiple Inheritance are
not supported by JAVA. Hence JAVA is known as Partially Object Oriented Programming
Language

Q) What is the difference between super class and subclass?

A super class is a class that is inherited whereas sub class is a class that does the inheriting.

Q) What modifiers may be used with top-level class?

Public, abstract and final can be used for top-level class


Objects and Classes
Q)What's the difference between constructors and other methods
Constructors must have the same name as the class and cannot return a value. They are only
called once while regular methods could be called many times.
Q)What is the difference between Overloading and Overriding
Overloading : Reusing the same method name with different arguments and perhaps a
different return type is called as overloading
Overriding : Using the same method name with identical arguments and return type is know
as overriding
Q)What do you understand by late binding or virtual method Invocation. (Example of
runtime polymorphism)
When a compiler for a non object oriented language comes across a method invocation, it
determines exactly what target code should be called and build machine language to
represent that call. In an object oriented language, this is not possible since the proper code
to invoke is determined based upon the class if the object being used to make the call, not the
type of the variable. Instead code is generated that will allow the decision to be made at run
time. This delayed decision making is called as late binding

Q)Can overriding methods have different return types


No they cannot have different return types
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q)If the method to be overridden has access type protected, can subclass have the
access type as private
No, it must have access type as protected or public, since an overriding method must not be
less accessible than the method it overrides
Q)Can constructors be overloaded
Yes constructors can be overloaded
Q)What happens when a constructor of the subclass is called
A constructor delays running its body until the parent parts of the class have been initialized.
This commonly happens because of an implicit call to super() added by the compiler. You can
provide your own call to super(arguments..) to control the way the parent parts are
initialized. If you do this, it must be the first statement of the constructor.
Q)If you use super() or this() in a constructor where should it appear in the constructor
It should always be the first statement in the constructor
Q) What is the first argument of the String array in main method?
The String array is empty. It does not have any element. This is unlike C/C++ where the first
element by default is the program name.
Q) If I do not provide any arguments on the command line, then the String array of Main
method will be empty or null?
It is empty. But not null.
Q) What environment variables do I need to set on my machine in order to be able to run
Java programs?
CLASSPATH and PATH are the two variables.

Q)What are local variables?


Local varaiables are those which are declared within a block of code like methods. Local
variables should be initialised before accessing them.
Q)What are instance variables?
Instance variables are those which are defined at the class level. Instance variables need not
be initialized before using them as they are automatically initialized to their default values.
Q)How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of the variable exists for
all instances of the class and the value can't be changed also.
static final int PI = 2.14; is an example for constant.
Q)Should a main method be compulsorily declared in all java classes?
No not required. main method should be defined only if the source class is a java application.
Q)What is the return type of the main method?
Main method doesn't return anything hence declared void.
Q)Why is the main method declared static?
main method is called by the JVM even before the instantiation of the class hence it is declared
as static.
Q)What is the arguement of main method?
main method accepts an array of String object as argument.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q)Can a main method be overloaded?


Yes. You can have any number of main methods with different method signature and
implementation in the
class.
Q)Can a main method be declared final?
Yes. Any inheriting class will not be able to have its own default main method.
Q)Does the order of public and static declaration matter in main method?
No it doesn't matter but void should always come before main ().
Q)Can a source file contain more than one Class declaration?
Yes a single source file can contain any number of Class declarations but only one of the class
can be declared as public.

Q)Why are there no global variables in Java?


Global variables are considered bad form for a variety of reasons: Adding state variables
breaks referential transparency (you no longer can understand a statement or expression on
its own: you need to understand it in the context of the settings of the global variables), State
variables lessen the cohesion of a program: you need to know more to understand how
something works. A major point of Object-Oriented programming is to break up global state
into more easily understood collections of local state, when you add one variable; you limit
the use of your program to one instance. What you thought was global, someone else might
think of as local: they may want to run two copies of your program at once. For these reasons,
Java decided to ban global variables.

Q)What does it mean that a class or member is final?

A final class can no longer be subclassed. Mostly this is done for security reasons with basic
classes like String and Integer. It also allows the compiler to make some optimizations, and
makes thread safety a little easier to achieve. Methods may be declared final as well. This
means they may not be overridden in a subclass. Fields can be declared final, too. However,
this has a completely different meaning. A final field cannot be changed after it’s initialized,
and it must include an initialize statement where it’s declared. For example, public final
double c = 2.998; It’s also possible to make a static field final to get the effect of C++’s const
statement or some uses of C’s #define, e.g. public static final double c = 2.998;

Q)What does it mean that a method or class is abstract?

An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate
that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component
{
Abstract classes may contain abstract methods. A method declared abstract is not actually
implemented in the current class. It exists only to be overridden in subclasses. It has no body.
For example,
public abstract float price();
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Abstract methods may only be included in abstract classes. However, an abstract class is not
required to have any abstract methods, though most of them do. Each subclass of an abstract
class must override the abstract methods of its superclasses or itself be declared abstract.

Q) What if the main method is declared as private?

The program compiles properly but at runtime it will give "Main method not public."
message.

Q)What are the differences between == and .equals() ?

Or What would you use to compare two String variables - the operator == or the
method equals()?

The == operator compares two objects to determine if they are the same object in memory
i.e. present in the same memory location. It is possible for two String objects to have the
same value, but located in different areas of memory.

== compares references while .equals compares contents. The method public boolean
equals(Object obj) is provided by the Object class and can be overridden. The default
implementation returns true only if the object is compared with itself, which is equivalent to
the equality operator == being used to compare aliases to the object. String, BitSet, Date, and
File override the equals() method. For two String objects, value equality means that they
contain the same character sequence. For the Wrapper classes, value equality means that the
primitive values are equal.

Inheritance

Q) What is interface and its use?

Interface is similar to a class which may contain method’s signature only but not bodies and it
is a formal set of method and constant declarations that must be defined by the class that
implements it. Interfaces are useful for: a)Declaring methods that one or more classes are
expected to implement b)Capturing similarities between unrelated classes without forcing a
class relationship. c)Determining an object’s programming interface without revealing the
actual body of the class.

Q)What is an abstract class?-

An abstract class is a class designed with implementation gaps for subclasses to fill in
and is deliberately incomplete.

Q)What is the difference between abstract class and interface?

a) All the methods declared inside an interface are abstract whereas abstract class
must have at least one abstract method and others may be concrete or abstract. b) In
abstract class, key word abstract must be used for the methods whereas interface we
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

need not use that keyword for the methods. c) Abstract class must have subclasses
whereas interface can’t have subclasses.
Q)What is an Abstract Class and what is it's purpose?
A Class which doesn't provide complete implementation is defined as an abstract class.
Abstract classes enforce abstraction.
Q)Can a abstract class be declared final?
Not possible. An abstract class without being inherited is of no use and hence will
result in compile time error.
Q)What is use of a abstract variable?
Variables can't be declared as abstract. only classes and methods can be declared as
abstract.
Q)Can you create an object of an abstract class?
Not possible. Abstract classes can't be instantiated.
Q)Can a abstract class be defined without any abstract methods?
Yes it's possible. This is basically to avoid instance creation of the class.

Class C implements Interface I containing method m1 and m2 declarations. Class C has


provided implementation for method m2. Can i create an object of Class C?
No not possible. Class C should provide implementation for all the methods in the
Interface I. Since Class C didn't provide implementation for m1 method, it has to be
declared as abstract. Abstract classes can't be instantiated.
Q)Can a method inside a Interface be declared as final?
No not possible. Doing so will result in compilation error. public and abstract are the
only applicable modifiers for method declaration in an interface.
Q)Can an Interface implement another Interface?
Intefaces doesn't provide implementation hence a interface cannot implement another
interface.
Q)Can an Interface extend another Interface?
Yes an Interface can inherit another Interface, for that matter an Interface can extend
more than one Interface.
Q)Can a Class extend more than one Class?
Not possible. A Class can extend only one class but can implement any number of
Interfaces.
Why is an Interface be able to extend more than one Interface but a Class can't extend
more than one Class?
Basically Java doesn't allow multiple inheritance, so a Class is restricted to extend only
one Class. But an Interface is a pure abstraction model and doesn't have inheritance
hierarchy like classes(do remember that the base class of all classes is Object). So an
Interface is allowed to extend more than one Interface.
Q)Can an Interface be final?
Not possible. Doing so so will result in compilation error.
Q)Can a class be defined inside an Interface?
Yes it's possible.
Q)Can an Interface be defined inside a class?
Yes it's possible.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q)Which OO Concept is achieved by using overloading and overriding?


Polymorphism.
Q)Why does Java not support operator overloading?
Operator overloading makes the code very difficult to read and maintain. To maintain
code simplicity, Java doesn't support operator overloading.
Q)Can we define private and protected modifiers for variables in interfaces?
No
Q)What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.
Q)Why isn't there operator overloading?
Because C++ has proven by example that operator overloading makes code almost
impossible to maintain.
Q)What restrictions are placed on method overriding?
Overridden methods must have the same name, argument list, and return type. The
overriding method may not limit the access of the method it overrides. The overriding
method may not throw any exceptions that may not be thrown by the overridden
method.
Q)What modifiers may be used with a top-level class?
A top-level class may be public, abstract, or final.
Q)Can a method be overloaded based on different return type but same argument type
?
No, because the methods can be called without using their return type in which case
there is ambiguity for the compiler
Q)What does it mean that a method or class is abstract?
An abstract class cannot be instantiated. Abstract methods may only be included in
abstract classes. However, an abstract class is not required to have any abstract
methods, though most of them do. Each subclass of an abstract class must override the
abstract methods of its superclasses or it also should be declared abstract.
Q) What are some alternatives to inheritance?
Delegation is an alternative to inheritance. Delegation means that you include an
instance of another class as an instance variable, and forward messages to the
instance. It is often safer than inheritance because it forces you to think about each
message you forward, because the instance is of a known class, rather than a new class,
and because it doesn't force you to accept all the methods of the super class: you can
provide only the methods that really make sense. On the other hand, it makes you
write more code, and it is harder to re-use (because it is not a subclass).
Q) What is the difference between an Interface and an Abstract class?
An abstract class can have instance methods that implement a default behavior. An
Interface can only declare constants and instance methods, but cannot implement
default behavior and all methods are implicitly abstract. An interface has all public
members and no implementation. An abstract class is a class which may have the usual
flavors of class members (private, protected, etc.), but has some abstract methods
Q) State the significance of public, private, protected, default modifiers both singly and
in combination and state the effect of package relationships on declared items
qualified by these modifiers.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

public : Public class is visible in other packages, field is visible everywhere (class must
be public too)
private : Private variables or methods may be used only by an instance of the same
class that declares the variable or method, A private feature may only be accessed by
the class that owns the feature.
protected : Is available to all classes in the same package and also available to all
subclasses of the class that owns the protected feature. This access is provided even to
subclasses that reside in a different package from the class that owns the protected
feature.
default :What you get by default ie, without any access modifier (ie, public private or
protected).It means that it is visible to all within a particular package.
Q) What is an abstract class?
Abstract class must be extended/subclassed (to be useful). It serves as a template. A
class that is abstract may not be instantiated (ie, you may not call its constructor),
abstract class may contain static data. Any class with an abstract method is
automatically abstract itself, and must be declared as such.
A class may be declared abstract even if it has no abstract methods. This prevents it
from being instantiated.
Q) What is final?
A final class can't be extended ie., final class may not be subclassed. A final method
can't be overridden when its class is inherited. You can't change value of a final
variable (is a constant).
Q) Can an application have multiple classes having main method?
Yes it is possible. While starting the application we mention the class name to be run.
The JVM will look for the Main method only in the class whose name you have
mentioned. Hence there is not conflict amongst the multiple classes having main
method.
Q)What is base class of all classes in java?
The base class is java.lang
which means java language
Q)Why does java not support multiple inheritance?
The fact is that Java separates implementation inheritance from interface inheritance.
Interface inheritance deals with the "contract" or "agreement " which a class should
adhere & on the other hand implementation inheritance deals with the reuse of code
and the contract which a class adheres to.
Q)What is polymorphism

Polymorphism means "many form". A object or data can behave differently at different
situation depend upon how we use that object or data. A good example of
polymorphism is method overriding.
A method with same name behave differently or give different output according to how
we invoked it.

Q)Does Java support multiple inheritance?


Java doesn't support multiple inheritance.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q)Why does it take so much time to access an Applet having Swing Components the first
time?

Because behind every swing component are many Java objects and resources. This
takes time to create them in memory. JDK 1.3 from Sun has some improvements which
may lead to faster execution of Swing applications.

Q)You can create a String object as String str = “abc”; Why cant a button object be
created as Button bt = “abc”;? Explain

The main reason you cannot create a button by Button bt1= “abc”; is because “abc” is a
literal string (something slightly different than a String object, by the way) and bt1 is a
Button object. The only object in Java that can be assigned a literal String is
java.lang.String. Important to note that you are NOT calling a java.lang.String
constuctor when you type String s = “abc”;

Q)What does the “abstract” keyword mean in front of a method? A class?

Abstract keyword declares either a method or a class. If a method has a abstract


keyword in front of it,it is called abstract method.Abstract method hs no body.It has
only arguments and return type.Abstract methods act as placeholder methods that are
implemented in the subclasses. Abstract classes can’t be instantiated.If a class is
declared as abstract,no objects of that class can be created.If a class contains any
abstract method it must be declared as abstract.
Q) When you declare a method as abstract method ?
When i want child class to implement the behavior of the method.
Q) Can I call a abstract method from a non abstract method ?
Yes, We can call a abstract method from a Non abstract method in a Java abstract class
Q) What is the difference between an Abstract class and Interface in Java ? or
can you explain when you use Abstract classes ?
Abstract classes let you define some behaviors; they force your subclasses to provide
others. These abstract classes will provide the basic funcationality of your applicatoin,
child class which inherited this class will provide the funtionality of the abstract methods
in abstract class. When base class calls this method, Java calls the method defined by the
child class.

An Interface can only declare constants and instance methods, but cannot implement
default behavior. Interfaces provide a form of multiple inheritance. A class can extend
only one other class.Interfaces are limited to public methods and constants with no
implementation. Abstract classes can have a partial implementation, protected parts,
static methods, etc. A Class may implement several interfaces. But in case of abstract
class, a class may extend only one abstract class. Interfaces are slow as it requires extra
indirection to find corresponding method in the actual class. Abstract classes are fast.
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

What methods java providing for Thread communications ?


Question
Answer : Java provides three methods that threads can use to communicate with each
other: wait, notify, and notifyAll. These methods are defined for all Objects
(not just Threads). The idea is that a method called by a thread may need to
wait for some condition to be satisfied by another thread; in that case, it can
call the wait method, which causes its thread to wait until another thread
calls notify or notifyAll.

What is the difference between notify and notify All methods ?


Q)
Answer A call to notify causes at most one thread waiting on the same object to be
notified (i.e., the object that calls notify must be the same as the object that
called wait). A call to notifyAll causes all threads waiting on the same
object to be notified. If more than one thread is waiting on that object,
there is no way to control which of them is notified by a call to notify (so it
is often better to use notifyAll than notify).

Q)Explain the Inheritance principle.

Inheritance is the process by which one object acquires the properties of


another object. Inheritance allows well-tested procedures to be reused and
enables changes to make once and have effect in all relevant places

Q)What are Encapsulation, Inheritance and Polymorphism

Or

Explain the Polymorphism principle. Explain the different forms of


Polymorphism.

Polymorphism in simple terms means one name many forms. Polymorphism


enables one entity to be used as a general category for different types of actions.
The specific action is determined by the exact nature of the situation.

Polymorphism exists in three distinct forms in Java:


• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface

q)Explain the Encapsulation principle.

Encapsulation is a process of binding or wrapping the data and the codes that operates
on the data into a single entity. This keeps the data safe from outside interface and
misuse. Objects allow procedures to be encapsulated with their data to reduce
potential interference. One way to think about encapsulation is as a protective
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Multithread programming

Q)What is the difference between process and thread?

Process is a program in execution whereas thread is a separate path of execution in a


program.
Q) Explain different way of using thread?
The thread could be implemented by using runnable interface or by inheriting from the
Thread class. The former is more advantageous, 'cause when you are going for multiple
inheritance..the only interface can help.

Q)What is multithreading and what are the methods for inter-thread communication
and what is the class in which these methods are defined?

Multithreading is the mechanism in which more than one thread run independent of
each other within the process. wait (), notify () and notifyAll() methods can be used for
inter-thread communication and these methods are in Object class. wait() : When a
thread executes a call to wait() method, it surrenders the object lock and enters into a
waiting state. notify() or notifyAll() : To remove a thread from the waiting state, some
other thread must make a call to notify() or notifyAll() method on the same object.

Q)What is the class and interface in java to create thread and which is the most
advantageous method?

Thread class and Runnable interface can be used to create threads and using Runnable
interface is the most advantageous method to create threads because we need not
extend thread class here.

Q)What are the states associated in the thread?

Thread contains ready, running, waiting and dead states.

Q)What is deadlock?

When two threads are waiting each other and can’t precede the program is said to be
deadlock.

Q)What is daemon thread and which method is used to create the daemon thread?

Daemon thread is a low priority thread which runs intermittently in the back ground
doing the garbage collection operation for the java runtime system. setDaemon method
is used to create a daemon thread.
Q) When a thread is created and started, what is its initial state?
A thread is in the ready state after it has been created and started.

Threads
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Q)Where does java thread support reside


It resides in three places
The java.lang.Thread class (Most of the support resides here)
The java.lang.Object class
The java language and virtual machine

Q)What is the difference between Thread and a Process


Threads run inside process and they share data.
One process can have multiple threads, if the process is killed all the threads inside it
are killed, they dont share data
Q)What happens when you call the start() method of the thread
This registers the thread with a piece of system code called thread scheduler
The schedulers determines which thread is actually running
Q)Does calling start () method of the thread causes it to run
No it merely makes it eligible to run. The thread still has to wait for the CPU time along
with the other threads, then at some time in future, the scheduler will permit the
thread to run
Q)When the thread gets to execute, what does it execute
The thread executes a method call run(). It can execute run() method of either of the
two choices given below :
The thread can execute it own run() method.
The thread can execute the run() method of some other objects
For the first case you need to subclass the Thread class and give your subclass a run()
method
For the second method you need to have a class implement the interface runnable.
Define your run method. Pass this object as an argument to the Thread constructor
Q)How many methods are declared in the interface runnable
The runnable method declares only one method :
public void run();
Q)Which way would you prefer to implement threading , by extending Thread class or
implementing Runnable interface
The preferred way will be to use Interface Runnable, because by subclassing the
Thread class you have single inheritance i.e you wont be able to extend any other class
Q)What happens when the run() method returns
When the run() method returns, the thread has finished its task and is considered
dead. You can’t restart a dead thread. You can call the methods of dead thread
Q)What are the different states of the thread
They are as follows:
Running: The state that all thread aspire to be
Various waiting states : Waiting, Sleeping, Suspended and Bloacked
Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

Ready : Waiting only for the CPU


Dead : All done
Q)What is Thread priority
Every thread has a priority, the higher priorit thread gets preference over the lower
priority thread by the thread scheduler
Q)What is the range of priority integer
It is from 1 to 10. 10 beings the highest priority and 1 being the lowest
Q)What is the default priority of the thread
The default priority is 5
Q)What happens when you call Thread.yield()
It caused the currently executing thread to move to the ready state if the scheduler is
willing to run any other thread in place of the yielding thread.
Yield is a static method of class Thread
Q)What is the advantage of yielding
It allows a time consuming thread to permit other threads to execute
Q)What happens when you call Thread.sleep()
It passes time without doing anything and without using the CPU. A call to sleep
method requests the currently executing thread to cease executing for a specified
amount of time.
Q)Does the thread method start executing as soon as the sleep time is over
No, after the specified time is over the thread enters into ready state and will only execute
scheduler allows it to do so.
Q)What do you mean by thread blocking
If a method needs to wait an indeterminable amount of time until some I/O occurrence takes p
a thread executing that method should graciously step out of the Running state. All java I/O
behave this way. A thread that has graciously stepped out in this way is said to be blocked
Q)What threading related methods are there in object class
wait(), notify() and notifyAll() are all part of Object class and they have to be called from syn
code only

Applet

Q)What is an applet?

Applet is a dynamic and interactive program that runs inside a web page displayed by
a java capable browser.

Q)What is the difference between applications and applets?


Skill base Lab course: OOP with Java Compiled by : Prof. Deepak Gaikar

a)Application must be run on local machine whereas applet needs no explicit


installation on local machine. b)Application must be run explicitly within a java-
compatible virtual machine whereas applet loads and runs itself automatically in a
java-enabled browser. d)Application starts execution with its main method whereas
applet starts execution with its init method. e)Application can run with or without
graphical user interface whereas applet must run within a graphical user interface.

Q) How does applet recognize the height and width?

Using getParameters() method.

Q) When do you use codebase in applet?

When the applet class file is not in the same directory, codebase is used.

Q)What is the lifecycle of an applet?

init() method - Can be called when an applet is first loaded start() method - Can be
called each time an applet is started. paint() method - Can be called when the applet is
minimized or maximized. stop() method - Can be used when the browser moves off the
applet’s page. destroy() method - Can be called when the browser is finished with the
applet.

Q)How do you set security in applets?

using setSecurityManager() method

Q)What is an event and what are the models available for event handling?

An event is an event object that describes a state of change in a source. In other words,
event occurs when an action is generated, like pressing button, clicking mouse,
selecting a list, etc. There are two types of models for handling events and they are: a)
event-inheritance model and b) event-delegation model

Q)What is meant by controls and what are different types of controls in AWT?

Controls are components that allow a user to interact with your application and the
AWT supports the following types of controls: Labels, Push Buttons, Check Boxes,
Choice Lists, Lists, Scrollbars, Text Components. These controls are subclasses of
Component.

You might also like