Core Java questions

1. What is JDK stands for?

JDK - Java Development Kit

2. How do you compile a Java Program?

> javac filename.java

3. What all the different OOPs concepts that Java supports?

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance 

4. Which provides runtime environment for java byte code to be executed? 

  1. JDK 
  2. JVM 
  3. JRE 
  4. JAVAC   

5. What is byte code in Java? 

  1. Code generated by a Java compiler
  2. Code generated by a JVM
  3. Name of the Java source code file
  4. Block of the code written in a class file

6. What will be the output of below program?

class demo {

    protected int x, y;

}

class Test {

    public static void main(String args[]) {

        demo t = new demo();

        System.out.println(t.x + " " + t.y);

    }

}

7. Why Java is not 100% Object Oriented ?

Java is not fully object oriented because it supports primitive data type like it,byte,long etc.,which are not objects. Because in JAVA we use data types like int, float, double etc which are not object oriented.

8. What is a Class in Java?

A class — in the context of Java — is a template used to create objects and to define object data types and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.  

9. How do you define an Object in Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes. 

10. What is Inheritance?

Inheritance is a mechanism in which one class acquires the property of another class. 

For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. 

11. Explain about Method Over Loading with an example?

Many methods may have similar names but different number or types of arguments.

Ex: public void login() 

      public void login(username, password)

12. Difference between Method Overloading and Method Overriding?

Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism.

In Method overloading many methods may have similar names but different number or types of arguments.

In Method overriding many methods may have a similar name and the same prototype.  

13. What is a constructor in Java?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.

14. What is the use of Exception Handling in Java?

The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained. 

15. What all Access Modifiers in Java?

  • Public
  • Private
  • Protected
  • Default

16. How do you define a method in Java?

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. 

17. Can we call a method from different package? if Yes, how?

18. What is a String in Java?

In Java, a string is a sequence of characters. For example, "hello" is a string containing a sequence of characters 'h' , 'e' , 'l' , 'l' , and 'o' . We use double quotes to represent a string in Java. 

Strings are objects. The Java platform provides the String class to create and manipulate strings. 

19. Write a sample Class in Java?

Class car { 

     String model; 

     speed() { 

          // The function of speed 

     }

 }  

20. How do you create an Object in Java?

Syntax to create an object

ClassName obj_name = new ClassName();

Car ford = new car(); 

Car audi = new car(); 

21. Difference between local variable and Instance variable?

Local variables: They are defined as a type of variable declared within programming blocks or methods. 

Instance variable: They are defined in class but outside the body of methods. 

22. What is meaning "Static" keyword?

The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. 

23. Explain about public static void main(String args[]) statement?

public static void main(String[] args) Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args) . You can only change the name of String array argument, for example you can change args to myStringArgs.

 public means You will access anywhere.

Static it mainly used for main method because we can call main method only one time which is fixed. 

void- void clarifies that the main method will not return any value. 

main- It's the name of the method. 

String[] args- Here we are defining a String array to pass arguments at command line. args is the variable name of the String array. It can be changed to anything such as String [] abc. 

24. What is the extension of Java Byte Code?

filename.class

25. What all the various Editors can be used for writing the Java Programs?

  • Eclipse IDE (Integrated Development Environment)
  • NetBeans
  • IntelliJ are some of the popular IDEs

26. What is Encapsulation with an example?

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP).  It describes the idea of bundling data and methods that work on that data within one unit.

e.g., a class in Java. This concept is also often used to hide the internal representation, or state, of an object from the outside.

27. What is Polymorphism in Java?

Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways. 

Compile-time polymorphism 

Example:  method overloading – Many methods may have similar names but different number or types of arguments.

Run-time polymorphism 

Example: method overriding – Many methods may have a similar name and the same prototype.

28. What is an Abstraction?

Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.

29. What is debugging?

Debugging is the routine process of identifying and removing bugs, errors or abnormalities from programs. It's a must have skill for any Java developer because it helps to find subtle bug that are not visible during code reviews or that only happens when a specific condition occurs. 

30. What is error in a program?

Errors are the problems or the faults that occur in the program, which makes the behavior of the program abnormal.

Programming errors are also known as the bugs or faults, and the process of removing these bugs is known as debugging. 

31. What all different exceptions you get in Java? provide some examples..

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. 

There are two types of exceptions in Java as follows:

  • Checked exception : Checked exceptions are exceptions that a Java application should be able to cope with. For example, If an application reads data from a file it should be able to handle the FileNotFoundException. 
  • Unchecked exception:
    • Errors : The second kind of exception is known as the error. When an exception occurs the JVM will create an exception object. These objects all derive from the Throwable class. 
    • Runtime exception: A runtime exception occurs simply because the programmer has made a mistake. You've written the code, it all looks good to the compiler and when you go to run the code, it falls over because it tried to access an element of an array that does not exist or a logic error caused a method to be called with a null value. 
    • Errors and Runtime Exceptions fall into the category of unchecked exceptions. 

32. What is collections in Java?

The Java collections framework provides a set of interfaces and classes to implement various data structures and algorithms