Cracking Dream Interview Tips-Part 2

Saumya Singh
Coding Blocks
Published in
10 min readNov 28, 2020

--

This blog is continuation of my previous blog in the Interview Preparation series 👩🏻‍💻 🚀.

In Part 2 of Cracking Dream Interview blog series, I will talk about “Must Practice Interview Questions of OOPs and Java | Top 35 ”.

1. What is Java?

Java is a high-level programming language.

It is cross-platform, object-oriented,multi-threaded programming language.

2. Can there be more than one main method in a Java Program?

Yes, you can have as many main methods as you like. You can have main methods with different signatures from main(String[]). This can be achieved with help of function overloading.

3. Can we execute a java program without a main method?

Yes, we can execute a java program without a main method by using a static block.

Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory

In the above example, we can execute a java program without a main method (works until Java 1.6 version). Java 7 and newer versions don’t allow this because JVM checks the presence of the main method before initializing the class.

The answer to this question depends on the version of java you are using. Prior to JDK 7, the main method was not mandatory in a java program.

4. Explain ‘public static void main(String[] args) in Java’.

main() in Java is the entry point for any Java program. It is always written as public static void main(String[] args).

  • public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
  • static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class.
  • void: It is the return type of the method. Void defines the method which will not return any value.
  • main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
  • String args[]: It is the parameter passed to the main method.

5. Why Java is platform independent?

Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

Platform independent

6. List any five features of Java?

Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded

7. Explain JVM, JDK and JRE.

My Notes 🙈
Important

8. List two Java IDE’s?

Netbeans, Eclipse, etc

9. Why Java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

10. What are wrapper classes in Java?

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it.

These are known as wrapper classes because they “wrap” the primitive data type into an object of that class.

11. What do you understand by Java virtual machine?

JVM is a virtual machine that enables the computer to run the Java program.

JVM acts like a run-time engine which calls the main method present in the Java code.

The Java code is compiled by JVM to be a Bytecode which is machine independent and close to the native code.

JVM Architecture

12. What is JIT compiler?

JIT- Just In Time Compiler

It is used to improve the performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.

13. Is Empty .java file name a valid source file name?

Yes, Java allows to save our java file by .java only, we need to compile it by javac .java and run by java classname

Let’s take a simple example:

14. What if I write static public void instead of public static void?

It is correct.

The program compiles and runs correctly because the order of specifiers doesn’t matter in Java.

15. What are static variables?

  • A static variable is common to all the instances (or objects) of the class because it is a class level variable.
  • Only a single copy of static variable is created and shared among all the instances(objects) of the class.
  • Memory allocation for such variables only happens once when the class is loaded in the memory.
static data_type variable_name;
  • Static Variable can be accessed directly in a static method, without object.

Static variable initialization

  1. Static variables are initialized when class is loaded.
  2. Static variables are initialized before any object of that class is created.
  3. Static variables are initialized before any static method of the class executes.

For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static.

16. What is OOPs?

It is a programming paradigm based on objects.

Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.

The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

17. What are Static Methods?

Static methods are the methods that can access class variables(static variables) without using object(instance) of the class, however non-static methods and non-static variables can only be accessed using objects.

static return_type method_name();

18. What is Singleton class?

Singleton class is a class whose only one instance can be created at any given time, in one JVM.

A class can be made singleton by making its constructor private.

19. What is the difference between Array list and vector in Java?

All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized.

Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.

Credits: Internet

20. What is synchronization in Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource.

Java Synchronization is better option where we want to allow only one thread to access the shared resource.

The synchronization is mainly used to

  1. To prevent thread interference.
  2. To prevent consistency problem.

Types of synchronization ?

There are two types of synchronization

  1. Process Synchronization
  2. Thread Synchronization
Tutorial Screen shots

“ Synchronization- Only one thread can access the resource at one time “

21. What are threads?

Threads are a light weight processes, they are most basic unit of processes.

Threads allows a program to operate more efficiently by doing multiple things at the same time.

Threads can be used to perform complicated tasks in the background without interrupting the main program.

Creating a Thread-

There are two ways to create a thread.

It can be created by extending the Thread class and implementing Runnable interface.

Thread lifecycle

22. What is difference between threads and processes?

Example-

  1. Let’s say House is a process and the people in house are threads. One house (process) can have multiple threads(people) in it. If you are living alone, it is single threaded else it is multi threaded, you will have to share resources with other housemates.
  2. Different tabs on your computer system are threads and whole browser is a single process.

23. Difference between Stack and Heap memory in Java.

24. What is a package in Java? List down various advantages of packages.

Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse.

  • Packages help in avoiding name clashes
  • They provide easier access control on the code
  • Packages can also contain hidden classes which are not visible to the outer classes and only used within the package

25. What is the difference between this() and super() in Java?

26. What is ‘this’ keyword in Java?

‘this’ keyword is used to access the instance of current class.

27. What is constructor chaining in Java?

The process of calling one constructor from another is called constructor chaining with respect to current object.

Constructor chaining can be achieved in two ways:

  1. Within the same class using this()
  2. From base class using super()

28. What is a classloader in Java?

It is subset of JVM (Java Virtual Machine). It is responsible for loading the class files.

29. Why Java Strings are immutable in nature?

String object cannot be changed in Java.

Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.

30. What is Arraylist?

The ArrayList is a resizable array, which can be found in the java.util package.

The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want.

Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

Features:

  • Java ArrayList allows us to randomly access the list.
  • ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases.
  • ArrayList in Java can be seen as a vector in C++.
  • ArrayList is not Synchronized. Its equivalent synchronized class in Java is Vector (vector is synchronized, that is at one time only one thread can access the shared resources).

Since ArrayList is a dynamic array and we do not have to specify the size while creating it, the size of the array automatically increases when we dynamically add and remove items.

31. What are interfaces in Java?

  • Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
  • Variables in interface are final, public and static.

It is a way of achieving multiple inheritance in Java.

interface <interface_name> {

// declare constant fields
// declare methods that abstract
// by default.
}
  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
  • To implement interface use implements keyword.

32. What are abstract classes?

A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).

We can not create object of abstract classes i.e it can not be instantiated.

33. What are collections in Java?

  • It is framework that provides architecture to manipulate and store data.
  • Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
  • Group of individual objects which are represented as a single unit is known as the collection of the objects.
  • Java collection framework includes the following: Interfaces, Classes and Methods

34. Explain multithreading.

Multithreading in Java is a process of executing multiple threads simultaneously.

Advantages of Java Multithreading

1) It doesn’t block the main task or main thread because threads are independent and you can perform multiple operations at the same time.

2) You can perform many operations together, so it saves time.

3) Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.

35. What is association, Aggregation, Composition in java?

Association-

Relation between two classes. (HAS-A Relation)

Aggregation-

Loose relation, one can exist without other. Both objects can exist independently e.g Driver and Car (both can exist without one another ). Aggregation is useful for code reusability.

Association-

Strong relation, both objects can not exist independently. One is dependent on other object e.g Car and Engine (engine can not exist without car)

Examples

Do clap 👏🏻👏🏻 50 times and share the article if you like it. Thank you for giving your valuable time

--

--

Saumya Singh
Coding Blocks

GHCI Scholar | International Open Source Award Finalist👩‍🎓 ️| SIH Winner