Java Interview Question and Answer SET 1



Core Java Placement Questions

1.      What happens when the following program is compiled and executed with the command - java test? Select the one correct answer.
class test
{
public static void main (String args[])
 {
     If (args.length > 0)
        System.out.println(args.length);
    }
}
(A) The program compiles and runs but does not print anything.
(B) The program compiles and runs and prints 0
(C)  The program compiles and runs and prints 1
(D) The program compiles and runs and prints 2

2.      What happens when the following program is compiled and then the command "java check it out" is executed. Select the one correct answer.
class check
{
public static void main(String args[])
{
System.out.println(args[args.length-2]);
}
}
   
 (A) The program compiles but generates ArrayIndexOutOfBoundsException exception 
 (B) The program prints java 
 (C)  The program prints check 
 (D) The program prints it



3.      What all gets printed when the following gets compiled and run. Select the two correct answers.
public class test {
    public static void main(String args[]) {
    String s1 = "abc";
    String s2 = "abc";
    if(s1 == s2)
        System.out.println(1);
    else
        System.out.println(2);
    if(s1.equals(s2))
        System.out.println(3);
    else
        System.out.println(4);
    }
}


(A) 1 2 

(B)  1 3 

(C)  1 4 
(D) 2 4


4.      What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test {
    public static void main(String args[]) {
    String s1 = "abc";
    String s2 = new String("abc");
    if(s1 == s2)
        System.out.println(1);
    else
        System.out.println(2);
    if(s1.equals(s2))
        System.out.println(3);
    else
        System.out.println(4);
    }
}
   
 (A) 1 3 
 (B) 1 4 
 (C)  2 3 
 (D) 2 4






5.      What would be the results of compiling and running the following class? Select the one correct answer.

class test
{
public static void main()
{
     System.out.println("test");
}
}
   
 (A) The program compiles and runs but does not generate any output. 
 (B) The program compiles and runs generating an output of "test" 
 (C)  The program compiles but does not run. 
 (D) The program does not compile as there is no main method defined

6.      What are difference between break and continue?

Answers:

The break keyword halts the execution of the current loop and forces control out of the loop.
The continue is similar to break, except that instead of halting the execution of the loop, it starts the next iteration.

7.      What is the difference between while statement and a do statement?

Answers:

A while statement checks at the beginning of a loop to see whether the next loop iteration should occur.

A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

8.      How can class be imported to a program?

Answers:

To import a class, the import keyword should be used as shown.;
import class name;

9.      Constructors can be overloaded like regular methods.
A) True
B) False

Answers: a.

10.  You would use the ____ operator to create a single instance of a named class.
a. New
b. dot

Answers: a.

11.  A constructor is automatically called when an object is instantiated.
a. true
b. false

Answers: a.


12.  What are packages? What is use of packages?

Answers:

The package statement defines a name space in which classes are stored. If you omit the package, the classes are put into the default package.
Signature... package pkg;

Use: * It specifies to which package the classes defined in a file belongs to. * Package is both naming and a visibility control mechanism.

13.  User-defined package can also be imported just like the standard packages.
True/False

Answers: True

14.  All standard classes of Java are included within a package called _____.

Answers: java.lang

15.  All the classes in a package can be simultaneously imported using ____.

Answers:   *

16.  What are different modifiers in java?

Answers: public, private, protected, default, static, transient, volatile, final, abstract.

17.  The Java language is:

a.       Interpreted at run time.
b.      Compiled to obtain executable target files.
c.       Designed for recursive string processing.
d.      Designed for matrix algorithm processing.

18.  In Java programs, the name of the class has to:

a.       Be the same as the name of the file it is saved in.
b.      Be different from the name of the file it is saved in.
c.       Be all capital letters.
d.      Be all small letters.

19.  Encapsulation refers to the idea that:

a.       Instance variables can only be accessed by means of methods.
b.      Methods can only be accessed by means of instance variables.
c.       All other methods must be defined within the main () method.
d.      Instance variables can only be declared within methods.

20.  In order to use objects of a given class the programmer needs to:
a.       Know what the methods do, but not how they do it.
b.      Know all of the instance variables of the class.
c.       Know how the method algorithms were implemented.
d.      Know the types of all of the instance variables of the class.

21.  What is the return type of program’s main ( ) method?

22.  What is the argument type of program’s main ( ) method?

23.  What is the use of bin and lib in JDK?

24.  What is the Extension of java byte codes?

A.        .java
B.        .class
C.        .bc
D.    None of the Above

25.  How many type of Primitive Data Types exist in Java?

5
7
8
10

Comments

Popular posts from this blog

Tips 01) self introduction in an interview

Computer Science Terminology

Why Failure Is Good for Success