Viva Questions All Chapter OOP JAVA (2024)
Viva Questions All Chapter OOP JAVA (2024)
Deepak Gaikar
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)
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
Package:
Q) What is Abstraction?
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.
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
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.
A super class is a class that is inherited whereas sub class is a class that does the inheriting.
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.
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;
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.
The program compiles properly but at runtime it will give "Main method not public."
message.
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
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.
An abstract class is a class designed with implementation gaps for subclasses to fill in
and is deliberately incomplete.
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.
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)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”;
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
Or
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 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 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
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.
When the applet class file is not in the same directory, codebase is used.
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)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.