java interview Questions
4.23K subscribers
61 photos
23 files
159 links
java interview questions for beginners
Any promotion for
rajp6133@gmail.com
Download Telegram
*Static Nested Class*

1) Static Nested ClassIn java static nested class is a class which is defined in a class with static keyword.


static nested class cannot access non-static data member and methods of outer class. It can access only static data member of outer class including private.

 Java Static Nested Class Example

class Simple
{
static int a = 700;
static class SimpleInner
{
void show()
{
System.out.println("value is"+a);
}
}
public static void main(String args[])
{
Simple.SimpleInner s = new Simple.SimpleInner();
s.show();
}
}
https://cjavapoint.blogspot.com/?m=1
*Non-Static Nested Class(inner class)*


*1) Member Inner Class*
 

In java member inner class is a class which is defined in class but outside of a method is called member inner class.

*Java Member Inner Class Example*

Java member inner class defined in a class but outside of method of that class.

In this example we will create outer class(MemberOuter) and inner class(MemberInner) and declare a private data member in outer class and access this private data member in inner class because inner class can access all the data members and methods in inner class, including private data member and methods.



class MemberOuter
{
private int salary = 4000;//private data member of outer class
class MemberInner
{
void get()
{
System.out.println("salary is "+salary);//access in inner class
}
}
public static void main(String args[])
{
MemberOuter mo = new MemberOuter();
MemberOuter.MemberInner mi = mo.new MemberInner();
mi.get();
}


output : salary is 4000 

*Note :*

(1)  In case of java outer and inner class, Here the java compiler creates two .class files the first is MemberOuter.class and MemberOuter$MemberInner.class.


(2) If you want to instantiate inner class, you must have to create the instance of outer class and instance of inner class is created inside the instance of outer class.

*2) Anonymous Inner Class*

In java anonymous inner class is a class that have no name is called anonymous inner class. This class can be instantiated only once. It is usually declared inside a method or block.


Anonymous inner class can be created by two ways in java 

Using class(abstract or concrete)
Using interface


*Java Anonymous Inner Class Example*

 abstract class AIExample
{
abstract void show();
}
class Test
{
public static void main(String args[])
{
AIExample a = new AIExample()
{
void show()
{
System.out.println("Method of anonymous class");
}
};
a.show();
}
}


output : Method of anonymous class 
 
The above example is performing anonymous by using abstract class.

*Java Anonymous Inner Class Example - Using Interface*

This is simple example of anonymous inner class using interface.
 
interface AnonymousExmple
{
void show();
}
 
class Test
{
public static void main(String args[])
{
AnonymousExmple ae = new AnonymousExmple()
{
public void show()
{
System.out.println("hello java ");
}
};
ae.show();
}
}


output : hello java

*3) Java Local Inner Class* 

In java local inner class is a class which is created inside a method is called local inner class in java. If you want to invokes the method of local inner class, you must instantiate this class inside the method.


*Java Local Inner Class Example*

class LICExample
{
private int a = 40;
void show()
{
class LICExample1
{
void get()
{
System.out.println("value is "+a);
}
}
LICExample1 l = new LICExample1();
l.get();
}
public static void main(String args[])
{
LICExample ll = new LICExample();
ll.show();
}
}

output : value is 40
https://cjavapoint.blogspot.com/?m=1
👍41
*Inner Class In Java*

Java inner class is a class which is defined in another class(outer class) is known as inner class or nested class in java.
 
*Syntax of Inner class or nested class :*
 
class OuterClass_Name
{
-----
-----
-----
class InnerClass_Name
{
-----
-----
-----
}


Java inner classes or nested classes can access all the data members and methods of its outer class including private data member and private methods.

There are a great relationship between java inner class and its outer class's instance.

Inner class is a part of nested class, non-static classes is known as inner class in java. 

Types of Nested Class

There are two types of nested class, non-static nested class and static nested class. The non-static nested class is known as inner class in java.


*(1) non-static nested class(inner class)*

1. member inner class

2. anonymous inner class

3. local inner class

*(2) static nested class*

4. static nested class
For more information visit:-
https://cjavapoint.blogspot.com/2023/06/A%20Basic%20Java%20Program%20For%20Beginners%202023.html
👍1
Java Inheritance Update 2023 With Program And Example
Java Inheritance Update 2023 With Program And Example nheritance is one of the essential concepts in object-orientated programming
(OOP) that lets in you to create a new magnificence this is based on an current elegance.
Inheritance allows you to define a brand new elegance (known as a subclass or derived magnificence) via inheriting
The homes and behaviors (fields and strategies) of an existing class (called a superclass or base magnificence).
This allows for code reuse and the introduction of a hierarchy of instructions.
Here's a simple Java program that demonstrates inheritance: // Superclass (Base class)
class Animal {
String name;

Animal(String name) {
this.name = name;
}

void eat() {
System.out.println(name + " is eating.");
}

void sleep() {
System.out.println(name + " is sleeping.");
}
}

// Subclass (Derived class)
class Dog extends Animal {
Dog(String name) {
super(name); // Call the constructor of the superclass
}

void bark() {
System.out.println(name + " is barking.");
}
}

// Subclass (Derived class)
class Cat extends Animal {
Cat(String name) {
super(name); // Call the constructor of the superclass
}

void meow() {
System.out.println(name + " is meowing.");
}
}

public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
Cat cat = new Cat("Whiskers");

dog.eat();
dog.sleep();
dog.bark();

cat.eat();
cat.sleep();
cat.meow();
}
} 👇👇👇👇👇👇   https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
👍2
import java.util.Arrays;
import java.util.LinkedHashSet;

public class ArrayExample
{
    public static void main(String[] args) throws CloneNotSupportedException
    {
        //Array with duplicate elements
        Integer[] numbers = new Integer[] {1,2,3,4,5,1,3,5};
        
        //This array has duplicate elements
        System.out.println( Arrays.toString(numbers) );
        
        //Create set from array elements
        LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(numbers) );
        
        //Get back the array without duplicates
        Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
        
        //Verify the array content
        System.out.println( Arrays.toString(numbersWithoutDuplicates) );
    }
}
1👍1
Java Interview Programs:
2)Fibonacci Series using recursion in java

class FibonacciExample2

static int n1=0,n2=1,n3=0;   
static void printFibonacci(int count)
{   
    if(count>0)
     {   
         n3 = n1 + n2;   
         n1 = n2;   
         n2 = n3;   
         System.out.print(" "+n3);  
         printFibonacci(count-1);   
     }   
}   
public static void main(String args[])
{   
  int count=10;   
  System.out.print(n1+" "+n2);
  //printing 0 and 1   
  printFibonacci(count-2);
//n-2 because 2 numbers are already printed  

}
1👍1
Java Interview Programs:
!) Fibonacci Series in Java without using recursion

class FibonacciExample1

public static void main(String args[]) 
{   
int n1=0,n2=1,n3,i,count=10;   
System.out.print(n1+" "+n2);

//printing 0 and 1   
   
for(i=2;i<count;++i)
//loop starts from 2 because 0 and 1 are already printed   
{   
  n3=n1+n2;   
  System.out.print(" "+n3);   
  n1=n2;   
  n2=n3;   
}   
 
}
}
2
All this program is very important for freshers
What do you understand by copy constructor in Java?
There is no copy constructor in java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
In this example, we are going to copy the values of one object into another using java constructor.
https://cjavapoint.blogspot.com/2023/09/Java%20Inheritance%20Update.html
1
What is JDBC?
JDBC is a Java API that is used to connect and execute the query to the database. JDBC API uses JDBC drivers to connect to the database. JDBC API can be used to access tabular data stored into any relational database.
What are the JDBC statements?
In JDBC, Statements are used to send SQL commands to the database and receive data from the database. There are various methods provided by JDBC statements such as execute(), executeUpdate(), executeQuery, etc. which helps you to interact with the database.
👍1
What is the return type of Class.forName() method?
The Class.forName() method returns the object of java.lang.Class object
👍1
How can we set null value in JDBC PreparedStatement?
By using setNull() method of PreparedStatement interface, we can set the null value to an index. The syntax of the method is given below.

void setNull(int parameterIndex, int sqlType) throws SQLException  
      
What is the role of the JDBC DriverManager class?
The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver().
What are the functions of the JDBC Connection interface?
The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that return the instance of Statement, PreparedStatement, CallableStatement, and DatabaseMetaData.
What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.
HashSet maintains no order whereas TreeSet maintains ascending order.
HashSet impended by hash table whereas TreeSet implemented by a Tree structure.
HashSet performs faster than TreeSet.
HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.
👍3
What is the difference between Set and Map?
The differences between the Set and Map are given below.
Set contains values only whereas Map contains key and values both.
Set contains unique values whereas Map can contain unique Keys with duplicate values.
Set holds a single number of null value whereas Map can include a single null key with n number of null values.