Java basics interview questions and answers

In this post we will come to know about the java basic interview questions and answers. This will helps to refresh basic interview questions knowledge before attending the interview. If you people are facing any extra questions in interview which is not present in this post or blog, please share through "Contact here" in the below gadget or drop me an email to "admin@javatbrains.com". 

Q1: Describe a java source file?
A: A java source file must have .java extension. It should have at least one top-level, public class definition and any number of non-public class definitions. The file name(without the extension) should match the public class name.

File: Public Class.java

     public class PubliClass{
        ....
     }

Q2: What is the ByteCode?
A: Each java program converted into one or more class files. The content of the source file is a set of instructions called ByteCode to be executed by Java Virtual Machine(JVM). JVM is an interpreter for byte code. Java introduces byte code to create Platform(hardware) independent program.

Q3: Why does the main method need a static identifier?
A: Because static method and members don't need an instance of their class to invoke them and main is the first method which is invoked.

Q4: Will the program compile, if the main method doesn't have static identifier?
A: It will compile. But, the program cannot be executed.

Q5: Which is the default parent class for a java class?
A: java.lang.Object. By default(implicit), all the java classes extend this class unless they have their own parent class. Also, the package java.lang is imported into all java files by default.

Q6: How are java objects passed to a method?
A: In java, both the primitive type and objects will be passed by value. During object passing, the object reference will be copied.

Q7: What are native methods?
A: Native methods are methods implemented in another programming language such as C. The Java Native Interface(JNI) is the API to invoke these native methods from a java class.

Q8: What is the order of declaring class, package and import statement with in a java file?
A: The order should be package, import statements and then class definition.

Q9: Which keywords are reserved?
A: goto and const are the 2 reserved keywords in java.

Q10: How are this and super keywords used?
A: 'this' keyword is used to refer to the current object instance. 'super' keyword is used to refer to the variables and methods of the super class of the current object instance.

Q11: Can a variable be an unsigned integer?
A: No, in java all the types are signed numeric numbers.

Q12: Can a private method be static?
A: Yes.

Q13: What are the various operators in java?
Q14: what is the use of instanceOf operator?
A: Above two questions answers i have already posted in operators in java.

Q14: Mention the access modifiers for a class, method and variable?
A: public: A class that is declared as public has global scope and an instance of this class can be created from anywhere within or outside of the class i.e. by other classes in the same package as well as classes in other packages. Only one non-inner class is any file can be defined with public keyword. The public modifier can be used for variable, methods and classes.

protected: A protected variable is visible with in the same class, same package and in sub classes in other packages. Any class in the same directory is considered to be in the default package, and thus protected classes will be visible. This can be used for methods, variables and inner classes.

default: A variable default no access modifier is said to have default visibility. Default visibility means a variable/method can be seen with in the same package, but not from other package.

private: Private variable are only visible from with in the same class. This means they are NOT visible within sub classes. This allows a variable to be insulated from being modified by any methods except those in the current class.
This can only be used in methods, variables and inner classes.

Q15: What is the widening conversion and narrowing conversion?
A: Widening conversion: This happens when we are trying to cast an object of lesser size to an object of larger size. ex: int -->long

Narrowing conversion: This happens when we are trying to cast object of larger size to an object of lesser size. ex: long --> int

Q16: Which is the most widely used protocol on internet?
A: Hyper Text Transfer Protocol (HTTP) is the most widely used protocol on internet. The text on internet is sent or received from one machine to another using this protocol only.

Q17: What is the difference between an executable file and .class file?
A: .exe file contains machine language instructions for the microprocessor and is system dependent. .class file contains byte code instructions for the JVM and is system independent.

Q18: Why java is suitable for internet?
A: Java is suitable for internet because of two reasons
1.It is system independent and hence its programs can run on any type of computer system available on internet.
2. It eliminated the lot of security problems for data on internet.

Q19: Why pointers are eliminated from Java?
A: 

  1. Pointers lead to confusion for a programmer.
  2. Pointers may crash a program easily, for example, when we add two pointers, the program crashes immediately. The same thing could also happen when we forgot to free the memory allotted to a variable and reallocate it to some other variable.
  3. Pointers break security. Using pointer, harmful programs like virus and other hacking programs can be developed.

Q20: What is the difference between method and function?
A: A method is a function that is written in a class. We do not have functions in java: instead we have methods. This means whenever a function is written in java, it should be written inside the class only. But we take C++, we can write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

Q21: What happen if String args[] is not written in main() method?
A: When main() method is written without String args[] as:
Public static void main()
The code will compile but JVM cannot run the code because it cannot recognize the main() method as the method from where it should start execution of the java program. Remember JVM always looks for main() method with String type array as parameter.

Q22: What are control statements?
A: This Question answer I have already posted in control statements.

Q23: What is the difference between return and System.exit(0)?
A: return statement is used inside a method to come out of it. System.exit(0) is used in any method to come out of the program.

Q24: What is the difference between System.exit(0) and System.exit(1)?
A: System.exit(0) terminates the program normally. Where as System.exit(1) terminates the program because of some error encountered in the system.

Q25: What is the difference between System.out and System.err?
A: System.out and System.err both represents the monitor by default and hence can be used to send data or results to the monitor. But System.out is used to display normal messages and results where as System.err is used to display error messages.

Related Topics:

Comments

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example