You are on page 1of 7

1) Explain Internal Architecture of JVM.

JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime
environment in which java bytecode can be executed.

• Classloader − Loads the class file into the JVM.


• Class Area −Storage areas for class elements structure like fields, method data, code
of method etc.
• Heap −Runtime storage allocation for objects.
• Stack −Storage for local variables and partial results. A stack contains frames and
allocates one for each thread. Once a thread get completed, this frame also gets
destroyed. It also plays roles in method invocation and returns.
• PC Registers −Program Counter Registers contains the address of instruction that
JVM is currently executing.
• Execution Engine −It has a virtual processor, interpreter to interpret bytecode
instructions one by one and a JIT, just in time compiler.
• Native method stack − It contains all the native methods used by the application.

2. What are the applications of Java?

• Mobile Applications
• Desktop GUI Applications
• Web-based Applications
• Enterprise Applications
• Scientific Applications
• Gaming Applications
• Big Data technologies
• Business Applications
• Distributed Applications
• Cloud-based Applications

3. Define variables. What are the types of variables?

A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.

Variable is a name of memory location. There are three types of variables in java: local,
instance and static.

There are two types of data types in Java: primitive and non-primitive.

Local Variable

A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.

A local variable cannot be defined with "static" keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called instance
variable. It is not declared as static.

It is called instance variable because its value is instance specific and is not shared among
instances.

3) Static variable

A variable which is declared as static is called static variable. It cannot be local. You can
create a single copy of static variable and share among all the instances of the class. Memory
allocation for static variable happens only once when the class is loaded in the memory.

4. Explain Type Casting with its syntax and example?

The process of converting the value of one data type (int, float, double, etc.) to another
data type is known as typecasting.

the major 2 types.

1.Widening Type Casting--Converting a lower data type into a higher one is called widening type
casting. It is also known as implicit conversion or casting down.

byte -> short -> char -> int -> long -> float -> double

2. Narrowing Type Casting-- Converting a higher data type into a lower one is called narrowing
type casting. It is also known as explicit conversion or casting up

double -> float -> long -> int -> char -> short -> byte
5. Explain Java String Methods with an example.

In Java, string is basically an object that represents sequence of char values. An array of
characters works same as Java string.

Java String class provides a lot of methods to perform operations on strings such as
compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring()
etc.

Method Description Return Type


Returns the character at the specified index
charAt() char
(position)
Returns the Unicode of the character at the specified
codePointAt() int
index
Returns the Unicode of the character before the
codePointBefore() int
specified index
Returns the Unicode in the specified text range of
codePointCount() int
this String
compareTo() Compares two strings lexicographically int
Compares two strings lexicographically, ignoring
compareToIgnoreCase() int
case differences
concat() Appends a string to the end of another string String
Checks whether a string contains a sequence of
contains() boolean
characters
Checks whether a string contains the exact same
contentEquals() sequence of characters of the specified boolean
CharSequence or StringBuffer
Returns a String that represents the characters of the
copyValueOf() String
character array
Checks whether a string ends with the specified
endsWith() boolean
character(s)
Compares two strings. Returns true if the strings are
equals() boolean
equal, and false if not
equalsIgnoreCase() Compares two strings, ignoring case considerations boolean
Returns a formatted string using the specified locale,
format() String
format string, and arguments
Encodes this String into a sequence of bytes using
getBytes() the named charset, storing the result into a new byte byte[]
array
getChars() Copies characters from a string to an array of chars void
hashCode() Returns the hash code of a string int
Returns the position of the first found occurrence of
indexOf() int
specified characters in a string
Returns the canonical representation for the string
intern() String
object
isEmpty() Checks whether a string is empty or not boolean
Returns the position of the last found occurrence of
lastIndexOf() int
specified characters in a string
length() Returns the length of a specified string int
Searches a string for a match against a regular
matches() boolean
expression, and returns the matches
Returns the index within this String that is offset
offsetByCodePoints() from the given index by codePointOffset code int
points
regionMatches() Tests if two string regions are equal boolean
Searches a string for a specified value, and returns a
replace() String
new string where the specified values are replaced
Replaces the first occurrence of a substring that
replaceFirst() matches the given regular expression with the given String
replacement
Replaces each substring of this string that matches
replaceAll() the given regular expression with the given String
replacement
split() Splits a string into an array of substrings String[]
Checks whether a string starts with specified
startsWith() boolean
characters
Returns a new character sequence that is a
subSequence() CharSequence
subsequence of this sequence
Extracts the characters from a string, beginning at a
substring() specified start position, and through the specified String
number of character
toCharArray() Converts this string to a new character array char[]
toLowerCase() Converts a string to lower case letters String
toString() Returns the value of a String object String
toUpperCase() Converts a string to upper case letters String
trim() Removes whitespace from both ends of a string String
Returns the string representation of the specified
valueOf() String
value

6. Explain increment and decrement operator with its types and example

class Operator {
public static void main(String[] args) {
int var1 = 5, var2 = 5;

// 5 is displayed
// Then, var1 is increased to 6.
System.out.println(var1++);

// var2 is increased to 6
// Then, var2 is displayed
System.out.println(++var2);
} }
the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator
-- decreases the value of a variable by 1.

7. Explain if statement with its syntax, control flow diagram and example.

An if statement consists of a Boolean expression followed by one or more statements.

Syntax

Following is the syntax of an if statement −

if(Boolean_expression) {
// Statements will execute if the Boolean expression is true
}

If the Boolean expression evaluates to true then the block of code inside the if statement will
be executed. If not, the first set of code after the end of the if statement (after the closing
curly brace) will be executed.

Flow Diagram

Example
Live Demo
public class Test {

public static void main(String args[]) {


int x = 10;

if( x < 20 ) {
System.out.print("This is if statement");
}
}
}
This will produce the following result −

Output
This is if statement.

8. Differentiate between conditional and looping statement.

A conditional Statement is a statement which is used in place of a if-else-if statement.

Syntax: (Condition)?(expression 1):(expression 2)

If the condition is true, it will execute expression 1 else it will execute expression 2.

A loop on the other hand will do the same instructions again and again until the condition is
satisfied. Loops include while, do while and for.

9. Explain Method Overriding in java with its example. What are the rules for

Rules for method overriding:

• In java, a method can only be written in Subclass, not in same class.


• The argument list should be exactly the same as that of the overridden method.
• The return type should be the same or a subtype of the return type declared in the original
overridden method in the super class.
• The access level cannot be more restrictive than the overridden method’s access level.
o For example: if the super class method is declared public then the over-ridding
method in the sub class cannot be either private or protected.
• Instance methods can be overridden only if they are inherited by the subclass.
• A method declared final cannot be overridden.
• A method declared static cannot be overridden but can be re-declared.
• If a method cannot be inherited then it cannot be overridden.
• A subclass within the same package as the instance’s superclass can override any superclass
method that is not declared private or final.
• A subclass in a different package can only override the non-final methods declared public or
protected.
• An overriding method can throw any uncheck exceptions, regardless of whether the
overridden method throws exceptions or not.
o However the overriding method should not throw checked exceptions that are new
or broader than the ones declared by the overridden method. The overriding
method can throw narrower or fewer exceptions than the overridden method.
• Constructors cannot be overridden.

Declaring a method in sub class which is already present in parent class is known as method
overriding. Overriding is done so that a child class can give its own implementation to a method
which is already provided by the parent class. In this case the method in parent class is called
overridden method and the method in child class is called overriding method.

You might also like