Webperco Soft Solutions - Interview Questions of Java
Webperco Soft Solutions - Interview Questions of Java
State (instance variables): Values assigned to the instance variables of an object make up the object's state.
Behavior (functions/methods): Methods/functions are where the class logic is stored. This is where the data gets
manipulated or algorithms get executed. Functions are also called procedures or methods.
In Method Overriding a subclass/child class has the same method name, same method signature and same return
types (covariant return types) as a method in its parent class, then the child class method has overridden the
parent class method.
You can find the complete detail about Method Overriding here.
A class with multiple methods by the same name but different parameters called Method Overloading.
You can find it in detail here.
Manipulators are the functions which can be used in conjunction with the insertion (<<) and extraction (>>)
operators on an object. Examples are end l and sets.
A constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation.
Rules for constructor are:
The ternary operator is said to be an operator which takes three arguments. Arguments and results are of different
data types, and it depends on the function. The ternary operator is also called a conditional operator.
7. Exception handling?
An exception is an event that occurs during the execution of a program. Exceptions can be of any type – Runtime
exception, Error exceptions. Those exceptions are adequately handled through exception handling mechanism like
try, catch, and throw keywords.
8. Destructor?
A destructor is a method which is automatically called when the object is made of scope or destroyed. Destructor
name is also same as class name but with the tilde symbol before the name.
An inline function is a technique used by the compilers and instructs to insert complete body of the function
wherever that function is used in the program source code .
Access modifiers determine the scope of the method or variables that can be accessed from other various objects
or classes. There are five types of access modifiers, and they are as follows:
Private
Protected
Public
Friend
Protected Friend
public class DuplicateElement {
Public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
System.out.println("Duplicate elements in given array: ");
//Searches for duplicate element
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] == arr[j])
System.out.println(arr[j]);
} } } }
public class LargestInArrayExample{
public static int getLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-1];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Largest: "+getLargest(a,6));
System.out.println("Largest: "+getLargest(b,7)); }}
OUTPUT:
Largest : 6
largest : 99