Java Basics Interview Questions

 Java Basics Interview Questions




Java Basics Interview Questions

Here are the most basic java interview questions, which you must know and all interviewers expect at least this from all candidates.



1) How many class definitions can be coded in one '.java' file?

Answer: As many as we want. but one is best practice


2) How many public class definitions can be coded in 1 .java file?

Answer: Only One


3) What should be the name of any .java file?

Answer: same as that of a public class name!

4) if no public class has been defined in the .java file, then what name can be given to the file?

Answer: any name can be given.


4) How many .class files are created when a.java is compiled successfully? 

Answer: as many class definitions exist in the java file.

5) How many class defn can contain the main()?

Answer: Our wish as many as we want.

6) Write a basic structure of a java program?

Answer:
public class MyClass
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

7) What should be given to compiler(javac) to compile? 

Answer: .java file name

8) What should be given to JVM (java) to execute?

Answer: class name.


9) Where to code the OOS for a program? 

Answer: main()


10) Is the main() method header fixed? If yes, why?

Answer: The header of the main method is fixed
public static void main(string[] args)
{
}

The main method has to strictly follow its syntax; otherwise, JVM will not be able to locate it and our program will not run.

public: They are public because they must be accessible to the JVM to begin execution of the program.
If it is not public then only the class and the package members can access it and JVM cannot.
Main is the first method that would get executed in any class. 

static: They are static because they must be available for execution without an object instance.

void: void means return type is nothing and  As soon as the main() method terminates, the java program terminates too. 
Hence, it doesn’t make any sense to return from the main() method as JVM can’t do anything with the return value of it.

main: It is the name of the Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.

String[] args: It stores Java command-line arguments and is an array of type "java.lang.String" class. 
Here, the name of the String array is args but it is not fixed and the user can use any name in place of it.


11) What can we change in the main method header and still have the class defn compile successfully?

a) change arg list
b) change ret d.t
c) change opt mod
d) change the id of the method
e) change access sp
answer: a


12) What can we change in the method header and still have the class defn compile and run successfully?

a) name of arg variable
b) order of public and static
c) [] ... => varargs
d) mark the main() final!
answer: a,b,c


13) Why should the main() declare void as return d.t?

Answer: void means return type is nothing and  As soon as the main() method terminates, the java program terminates too. 
Hence, it doesn’t make any sense to return from the main() method as JVM can’t do anything with the return value of it.


14) Why should the main() be declared static?

Answer: They are static because they must be available for execution without an object instance.
and, we want the JVM to enter the main() without
creating an object of enclosing class.


15) What is the reason to pass String[] to main()?

Answer: to accept command-line arguments.


16) Explain the lifecycle of a java program / JVM?

Answer: starts with main and end when all thread execution is completed or "System.exit()" called.


17) What is the flow control of the Java program?

Answer: it is the path the JVM takes through our code…!
Source file(.java)=>javac(javacompiler)=>ByteCodeFile(.class)=>JVM=>OS



18) Can a class be declared static? Who can be declared static? What does it mean?

Answer: no class can't be declared static, methods/initializer/variable can be declared static, it means the method is available for execution without any object instance.


19) What mem areas are created by JVM when it starts?

Answer:
heap
stack
misc
other


20) Is "main" a keyword? 

Answer: no it is an identifier for JVM.


What is a java program?

Answer: a collection of class definitions (in many .java files) where at least 1 of them contains main().

Bonus notes -Best Practices:

1. What are naming conventions and coding conventions 

followed by everyone?

Naming Conventions are:

Class: Start with uppercase + Camel case (Ex. Hello, HelloWorld, MyFirstProgram)

variable and method: Starts with lower case + Camel case. (Ex. isPrime, bark, eat, isSumGreaterThanDiffrence)

package: reverse domain name + team/project (Ex com.getwayssolution.a1.coll, com.yourname.batch.pack)

Coding convention:
=>check for JDK
=>TEST
=>SET DEVLOPMENT ENVIRONMENT
=>Source(.java)
=>classes(.class)
=>library
=>code
=>test


2) How many class defn should be code per file and what should be its access specifier?

Answer: As many as we want but more than one public class is not allowed.


3) How to decide how many .java files to create per java program?

Answer: based on requirements


4) How many classes will usually contain main()

Answer: usually it is one but we can have any no of classes contain main().









1 comment:

For Query and doubts!

Powered by Blogger.