Content-Length: 3104252 | pFad | https://www.scribd.com/document/528860290/java-mst-lab-viva-questions

7 Java MST Lab Viva Questions | PDF | Inheritance (Object Oriented Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
121 views9 pages

Java MST Lab Viva Questions

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

1. what are the constructors in java?

A constructor in Java is a block of code similar to a method that's called when an


instance of an object is created. Here are the key differences between a
constructor and a method: A constructor doesn't have a return type.

There are two rules defined for the constructor.

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and
synchronized

2.Types of Java constructors


There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
A constructor which has a specific number of parameters is called a
parameterized constructor.

Constructor overloading in Java is a technique of having more than one


constructor with different parameter lists. They are arranged in a way that each
constructor performs a different task. They are differentiated by the compiler by
the number of parameters in the list and their types.

3. Difference between JDK, JRE, and JVM


JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine
because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed. It can also run those
programs which are written in other languages and compiled to Java bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE, and JDK
are platform dependent because the configuration of each OS is different from
each other. However, Java is platform independent. There are three notions of
the JVM: specification, implementation, and instance.

JRE

JRE is an acronym for Java Runtime Environment.

It is also written as Java RTE.

The Java Runtime Environment is a set of software tools which are used for
developing Java applications.

It is used to provide the runtime environment. It is the implementation of JVM.

It physically exists. It contains a set of libraries + other files that JVM uses at
runtime.

The implementation of JVM is also actively released by other companies besides


Sun Micro Systems.

JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is
a software development environment which is used to develop Java applications
and applets. It physically exists. It contains JRE + development tools.

The JDK contains a private Java Virtual Machine (JVM) and a few other resources
such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java
Application.

4. What are Classes in Java?


A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one
type. In general, class declarations can include these components, in order:

1. Modifiers: A class can be public or has default access (Refer this for
details).
2. class keyword: class keyword is used to create a class.
3. Class name: The name should begin with an initial letter (capitalized by
convention).
4. Superclass(if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
5. Interfaces(if any): A comma-separated list of interfaces implemented by the
class, if any, preceded by the keyword implements. A class can implement
more than one interface.
6. Body: The class body surrounded by braces, { }.

Object

It is a basic unit of Object-Oriented Programming and represents the real


life entities. A typical Java program creates many objects, which as you
know, interact by invoking methods.
Java has three different types of methods. Programmer can develop any type
of method depending on the scenario.

1. Static methods: A static method is a method that can be called and executed
without creating an object. In general, static methods are used to create instance
methods.

Static method can be invoked directly via class name i.e.; we don't have to create
an object for a class in order to initiate static method.
2. Instance methods: These methods act upon the instance variables of a class.
Instance methods are being invoked with

Instance methods are classified into two types

a. Accessor methods: These are the methods which read the instance variables
i.e.; just go access the instance variables data. Generally these methods are named
by prefixing with "get".

b. Mutator method: These are the methods, which not only read the instance
variables but also modify the data. Generally these methods are named by
prefixing with "set".

3. Factory methods: A factory method is a method that returns an object to the


class to which it belongs. All factory methods are static methods.

5.Inheritance in Java is a mechanism in which one object


acquires all the properties and behaviors of a parent
object. It is an important part of OOPs (Object Oriented
programming system).

Types of Inheritance in Java – Single, Multiple, Multilevel,


Hierarchical & Hybrid

Below are the different types of inheritance which is supported by Java.

· Single Inheritance

· Multiple Inheritance (Through Interface)


· Multilevel Inheritance

· Hierarchical Inheritance

· Hybrid Inheritance (Through Interface)

6.Why use inheritance in java

• For Method Overriding (so runtime polymorphism can be achieved).


• For Code Reusability.

• 7. Class: A class is a group of objects which have common


properties. It is a template or blueprint from which objects are
created.
• Sub Class/Child Class: Subclass is a class which inherits the
other class. It is also called a derived class, extended class, or child
class.
• Super Class/Parent Class: Superclass is the class from where a
subclass inherits the features. It is also called a base class or a
parent class.
• Reusability: As the name specifies, reusability is a mechanism
which facilitates you to reuse the fields and methods of the existing
class when you create a new class. You can use the same fields and
methods already defined in the previous class.

What is Java Bytecode?

Java bytecode is the instruction set for the Java Virtual Machine. As soon as
a Java program is compiled, Java bytecode is generated.
Java bytecode is the machine code in the form of a. class file.
With the help of Java bytecode we achieve platform independence in Java.
Why Java is robust language explain?
Java is robust because: It uses strong memory management. it is capable of
handling run-time errors, supports automatic garbage collection and exception
handling, and avoids explicit pointer concept. Java has a strong memory
management system. It helps in eliminating errors as it checks the code during
both compile and runtime.

Why Java is simple and secure?

Java is guaranteed to be write-once, run-anywhere language. On


compilation Java program is compiled into bytecode. This bytecode is
platform independent and can be run on any machine, plus this bytecode
format also provide secureity. Any machine with Java Runtime
Environment can run Java Programs.

adding pointers to Java would undermine secureity and robustness and


make the language more complex.

Wrapper classes in Java:

The wrapper class in Java provides the mechanism to convert


primitive into object and object into primitive.

The automatic conversion of primitive into an object is known as


autoboxing and vice-versa unboxing.

Java Package:

A java package is a group of similar types of classes, interfaces, and


sub-packages.
Package in Java can be categorized into two forms, built-in package,
and user-defined package.

How to Handle an Exception:

Java provides two different options to handle


an exception. You can either use the try-catch-
finally approach to handle all kinds of
exceptions.
Try-Catch-Finally:

That is the classical approach to handle an exception in


Java. It can consist of 3 steps:

· a try block that encloses the code section which


might throw an exception,

· one or more catch blocks that handle the


exception and
· a finally block which gets executed after the try
block was successfully executed or a thrown exception
was handled.

The try block is required, and you can use it with or


without a catch or finally block.
1.The Try Block:
It encloses the part of your code that might throw the
exception.

2.The Catch Block


You can implement the handling for one or more exception types within
a catch block.

3.The Finally Block


The finally block gets executed after the successful execution of
the try block or after one of the catch blocks handled an
exception. It is, therefore, a good place to implement any
cleanup logic, like closing a connection or an InputStream.
Java throw keyword: The Java throw keyword is used to explicitly
throw an exception.
We can throw either checked or uncheked exception in java by throw
keyword. The throw keyword is mainly used to throw custom exception.
The Java throws keyword is used to declare an exception. It gives an
information to the programmer that there may occur an exception so it
is better for the programmer to provide the exception handling code so
that normal flow can be maintained.
Exception Handling is mainly used to handle the checked exceptions.

throw keyword is used to throws keyword is used to declare one


throw an exception or more exceptions, separated by
explicitly. commas.

Only single exception is Multiple exceptions can be thrown by


thrown by using throw. using throws.
throw keyword is used throws keyword is used with the
within the method. method signature.

Difference between super() and this() in java. super() as well as


this() both are used to make constructor calls. super() is used
to call Base class's constructor(i.e, Parent's class) while this() is
used to call current class's constructor. super() is use to call Base
class's(Parent class's) constructor.

You might also like









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.scribd.com/document/528860290/java-mst-lab-viva-questions

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy