Java Que Bank Solve
Java Que Bank Solve
Java Que Bank Solve
Unit-1
ii) JVM:
What is JVM A specification where working of Java Virtual
Machine is specified. But implementation provider is independent
to choose the algorithm. Its implementation has been provided by
Oracle and other companies.
An implementation Its implementation is known as JRE (Java
Runtime Environment). Runtime Instance Whenever you write
java command on the command prompt to run the java class, an
instance of JVM is created.
In any Java program, the main() method is the starting point from where
compiler starts program execution. So, the compiler needs to call the main()
method.
If the main() is allowed to be non-static, then while calling the main()
method JVM has to instantiate its class.
While instantiating it has to call the constructor of that class, There will be
ambiguity if the constructor of that class takes an argument.
Static method of a class can be called by using the class name only without
creating an object of a class.
The main() method in Java must be declared public, static and void. If any
of these are missing, the Java program will compile but a runtime error will
be thrown.
Unit-2
2. Literal: A literal is a value that is directly used in the program, without being
stored in a variable. In Java, literals can take various forms depending on the
data type they represent, such as integer literals, floating-point literals,
boolean literals, and string literals.
int age = 25; // Integer literal
double price = 29.99; // Floating-point literal
boolean isRaining = true; // Boolean literal
String name = "John"; // String literal
• A for loop is a loop that runs for a preset number of times.It has three
parts: an initialization, a condition, and an update. For example:
// This while loop prints "Hello" until the user enters 'q'
char input;
while (input != 'q') {
printf("Hello\n");
scanf("%c", &input);
}
Ex:
int[] numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
Array.Sort(numbers);
foreach (int number in numbers)
{
Console.Write(number + " ");
}
// Output: 1 1 2 3 4 5 5 6 9
In the above example, the Array.Sort() method is called on the numbers
array, which sorts the elements in ascending order. The sorted array is then
output to the console using a foreach loop.
Array.Fill():
The Array.Fill() method is used to fill all the elements in an array with a
specified value. It takes an array as input, as well as the value to fill the
array with, and the index to start filling from (optional). Here is an example
of using Array.Fill() to fill an array of strings with the value "Hello World":
Ex:
string[] greetings = new string[5];
Array.Fill(greetings, "Hello World");
foreach (string greeting in greetings)
{
Console.Write(greeting + " ");
}
// Output: Hello World Hello World Hello World Hello World Hello World
In the above example, the Array.Fill() method is called on the greetings
array, which fills all the elements in the array with the string "Hello World".
The filled array is then output to the console using a foreach loop.
Array.BinarySearch():
The Array.BinarySearch() method is used to search for a specified value in a
sorted array. It takes an array as input, as well as the value to search for,
and returns the index of the value in the array (if found), or a negative
number if the value is not found. Here is an example of using
Array.BinarySearch() to search for the value 5 in a sorted array of integers:
Ex:
int[] numbers = { 1, 1, 2, 3, 4, 5, 5, 6, 9 };
int index = Array.BinarySearch(numbers, 5);
Console.WriteLine(index);
// Output: 5
In the above example, the Array.BinarySearch() method is called on the
numbers array to search for the value 5. Since the array is sorted, the
method returns the index of the first occurrence of 5 in the array, which is
5. The index is then output to the console using a Console.WriteLine()
statement.
Unit -3
There are two types of modifiers in Java: access modifiers and non-access
modifiers.
Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any
access level, it will be the default.
Protected: The access level of a protected modifier is within the package
and outside the package through child class. If you do not make the child
class, it cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package and
outside the package.
2) Discuss Final and static method with example.
Ans:
In Java, final and static are two keywords that can be used to modify
methods.
A final method is a method that cannot be overridden in a subclass. This means
that if a subclass defines a method with the same name and signature as a final
method in its superclass, it will not be able to override it. This is often used to
prevent subclasses from changing the behavior of a particular method that is
critical to the class's functionality.
Thread life cycle in Java refers to the various states that a thread can
be in at any given time. These states include:
1. New: A thread is in the "new" state when it has been created but has not yet
started running.
2. Runnable: A thread is in the "runnable" state when it has been started and is
ready to run, but the scheduler has not yet chosen it to run.
3. Running: A thread is in the "running" state when the JVM has selected it to run
on a CPU.
4. Blocked: A thread is in the "blocked" state when it is waiting for a resource to
become available, such as a lock on an object.
The idea behind inheritance in Java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class, you
can reuse methods and fields of the parent class. Moreover, you can add
new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
Types of constructors:
// default constructor
public Person() {
name = "";
age = 0;
}
}
// parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// copy constructor
public Person(Person p) {
this.name = p.name;
this.age = p.age;
}
}
// private constructor
private Singleton() {
}
To import a package in Java, you can use the import statement. This
statement tells the Java compiler which package to search for the
classes and interfaces that you want to use in your code.
Here's an example of how to import a package:
import com.example.util.*;
// code here
}
9) What is Variable? Explain different types of variable.
Ans:
Ans:
When the child class extends from more than one superclass, it is known
as multiple inheritance. However, Java does not support multiple inheritance.
To achieve multiple inheritance in Java, we must use the interface.
"interface Backend {
// abstract class
public void connectServer();
}
class Frontend {
Unit-4
1) Differentiate Checked and unchecked Exception.
Ans:
The try block is used to enclose the code that might generate an exception.
If an exception occurs in the try block, the control is transferred to the catch
block. The catch block is used to handle the exception that occurred in the
try block.
try {
System.out.print("Enter numerator: ");
num = sc.nextInt();
System.out.print("Enter denominator: ");
denom = sc.nextInt();
} catch (ArithmeticException e) {
// Catch divide-by-zero exception
System.out.println("Error: Cannot divide by zero!");
} finally {
// Close scanner object
sc.close();
}
}
}
_______________
Abhishek.