Java Screen Test Questions For Interview.
Java Screen Test Questions For Interview.
There are soame random core java objective question and answers are
given for pratice test for freshers orlearning java developers. I hope this
will refresh youyour skill and helpful to clear java test in Java Interview for
your job search.
given for pratice test for freshers orlearning java developers. I hope this
will refresh youyour skill and helpful to clear java test in Java Interview for
your job search.
Q 1. What is the output of the following
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
System.out.println("Poddar".substring(3));
Ans:
a) false
false
false
dar
b) false
true
false
Poddar
c) Compiler Error
d) true
true
false
dar
Correct Answer is a)
***** Look carefully at code and answer the following
Q2. What is the output of following if the return value is "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument" (Assuming written inside main)
String s5 = "AMIT";
String s6 = "amit";
System.out.println(s5.compareTo(s6));
System.out.println(s6.compareTo(s5));
System.out.println(s6.compareTo(s6));
Ans
a> -32
32
0
b> 32
32
0
c> 32
-32
0
d> 0
0
0
Correct Answer is a)
Q3) What is the output (Assuming written inside main)
String s1 = new String("amit");
String s2 = s1.replace('m','i');
s1.concat("Poddar");
System.out.println(s1);
System.out.println((s1+s2).charAt(5));
a) Compile error
b) amitPoddar
o
c) amitPoddar
i
d) amit
i
Correct answer is d)
As String is imutable.so s1 is always "amit". and s2 is "aiit".
Q4) What is the output (Assuming written inside main)
String s1 = new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
System.out.println(s3==s4);
a) arit
amit
false
true
b) arit
arit
false
true
c) amit
amit
false
true
d) arit
amit
true
true
Correct answer is a)
s3==s4 is true because java points both s3 and s4 to same memory location in string pool
Q5) Which one does not extend java.lang.Number
1)Integer
2)Boolean
3)Character
4)Long
5)Short
Correct answer is 2) and 3)
Q6) Which one does not have a valueOf(String) method
1)Integer
2)Boolean
3)Character
4)Long
5)Short
Correct answer is 3)
Q.7) What is the output of following (Assuming written inside main)
String s1 = "Amit";
String s2 = "Amit";
String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(s1.equals(s2));
System.out.println((s1==s2));
System.out.println(s3.equals(s4));
System.out.println((s3==s4));
a) true
true
true
false
b) true
true
true
true
c) true
false
true
false
Correct answer is a)
Q8) What will be the output of line 5
1 Choice c1 = new Choice();
2 c1.add("First");
3 c1.addItem("Second");
4 c1.add("Third");
5 System.out.println(c1.getItemCount());
a) 1
b) 2
c) 3
d) None of the above
Correct Answer is c)
Q9) What will be the order of four items added
Choice c1 = new Choice();
c1.add("First");
c1.addItem("Second");
c1.add("Third");
c1.insert("Lastadded",2);
System.out.println(c1.getItemCount());
a) First,Second,Third,Fourth
b) First,Second,Lastadded,Third
c) Lastadded,First,Second,Third
Correct ANswer is b)
Q10) Answer based on following code
1 Choice c1 = new Choice();
2 c1.add("First");
3 c1.addItem("Second");
4 c1.add("Third");
5 c1.insert("Lastadded",1000);
6 System.out.println(c1.getItemCount());
a) Compile time error
b) Run time error at line 5
c) No error and line 6 will print 1000
d) No error and line 6 will print 4
Correct ANswer is d)
Q11). What will be the output of follwing
{
double d1 = -0.5d;
System.out.println("Ceil for d1 " + Math.ceil(d1));
System.out.println("Floor for d1 " +Math.floor(d1));
}
Answers:
a) Ceil for d1 0
Floor for d1 -1;
b) Ceil for d1 0
Floor for d1 -1.0;
c) Ceil for d1 0.0
Floor for d1 -1.0;
d) Ceil for d1 -0.0
Floor for d1 -1.0;
correct answer is d) as 0.0 is treated differently from -0.0
Q12). What is the output of following
{
float f4 = -5.5f;
float f5 = 5.5f;
float f6 = -5.49f;
float f7 = 5.49f;
System.out.println("Round f4 is " + Math.round(f4));
System.out.println("Round f5 is " + Math.round(f5));
System.out.println("Round f6 is " + Math.round(f6));
System.out.println("Round f7 is " + Math.round(f7));
}
a)Round f4 is -6
Round f5 is 6
Round f6 is -5
Round f7 is 5
b)Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5
Correct answer is b)
Q13). Given Integer.MIN_VALUE = -2147483648
Integer.MAX_VALUE = 2147483647
What is the output of following
{
float f4 = Integer.MIN_VALUE;
float f5 = Integer.MAX_VALUE;
float f7 = -2147483655f;
System.out.println("Round f4 is " + Math.round(f4));
System.out.println("Round f5 is " + Math.round(f5));
System.out.println("Round f7 is " + Math.round(f7));
}
a)Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -2147483648
b)Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -2147483655
correct answer is a)
//Reason If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is
equal to the value of Integer.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is
equal to the value of Integer.MAX_VALUE. // From JDK api documentation
Q14)
1 Boolean b1 = new Boolean("TRUE");
2 Boolean b2 = new Boolean("true");
3 Boolean b3 = new Boolean("JUNK");
4 System.out.println("" + b1 + b2 + b3);
a) Comiler error
b) RunTime error
c)truetruefalse
d)truetruetrue
Correct answer is c)
Q15) In the above question if line 4 is changed to
System.out.println(b1+b2+b3); The output is
a) Compile time error
b) Run time error
c) truetruefalse
d) truetruetrue
Correct answer is a) As there is no method to support Boolean + Boolean
Boolean b1 = new Boolean("TRUE");
Think ----->System.out.println(b1); // Is this valid or not?
Q16). What is the output
{
Float f1 = new Float("4.4e99f");
Float f2 = new Float("-4.4e99f");
Double d1 = new Double("4.4e99");
System.out.println(f1);
System.out.println(f2);
System.out.println(d1);
}
a) Runtime error
b) Infinity
-Infinity
4.4E99
c) Infinity
-Infinity
Infinity
d) 4.4E99
-4.4E99
4.4E99
Correct answer is b)
Q17. Which of the following wrapper classes can not
take a "String" in constructor
1) Boolean
2) Integer
3) Long
4) Character
5) Byte
6) Short
correct answer is 4)
Q18. What is the output of following
Double d2 = new Double("-5.5");
Double d3 = new Double("-5.5");
System.out.println(d2==d3);
System.out.println(d2.equals(d3));
a) true
true
b) false
false
c) true
false
d) false
true
Correct answer is a)
Q19) Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
\dir1\IO.class -- IO.class is under dir1
Consider the following code
import java.io.*;
public class IO {
public static void main(String args[]) {
File f = new File("..\\12345.msg");
try{
System.out.println(f.getCanonicalPath());
System.out.println(f.getAbsolutePath());
}catch(IOException e){
System.out.println(e);
}
}
}
What will be the output of running "java IO" from C:\java\dir1
a) C:\java\12345.msg
C:\java\dir1\..\12345.msg
b) C:\java\dir1\12345.msg
C:\java\dir1\..\12345.msg
c) C:\java\dir1\..\12345.msg
C:\java\dir1\..\12345.msg
correct answer is a) as getCanonicalPath Returns the canonical form of this File object's pathname. The precise definition of canonical form is system-dependent, but it usually
specifies an absolute pathname in which all relative references and references to the current user directory have been completely resolved.
WHERE AS
getAbsolutePath Returns the absolute pathname of the file represented by this object. If this object represents an absolute pathname, then return the pathname. Otherwise, return a pathname that is a concatenation of the current user directory, the separator character, and the pathname of this file object.
Q20) Suppose we copy IO.class from C:\java\dir1 to c:\java
What will be the output of running "java IO" from C:\java.
a) C:\java\12345.msg
C:\java\..\12345.msg
b) C:\12345.msg
C:\java\..\12345.msg
c) C:\java\..\12345.msg
C:\java\\..\12345.msg
correct answer is b)
Q21) Which one of the following methods of java.io.File throws IOException and why
a) getCanonicalPath and getAbsolutePath both require filesystem queries.
b) Only getCannonicalPath as it require filesystem queries.
c) Only getAbsolutePath as it require filesystem queries.
Correct answer is b)
Q22) What will be the output if
Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
\dir1\IO.class -- IO.class is under dir1
import java.io.*;
public class IO {
public static void main(String args[]) {
File f = new File("12345.msg");
String arr[] = f.list();
System.out.println(arr.length);
}
}
a) Compiler error as 12345.msg is a file not a directory
b) java.lang.NullPointerException at run time
c) No error , but nothing will be printed on screen
Correct ansewer is b)
Q23) What will be the output
Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
import java.io.*;
public class IO {
public static void main(String args[]) {
File f1 = new File("\\12345.msg");
System.out.println(f1.getPath());
System.out.println(f1.getParent());
System.out.println(f1.isAbsolute());
System.out.println(f1.getName());
System.out.println(f1.exists());
System.out.println(f1.isFile());
}
}
a) \12345.msg
\
true
12345.msg
true
true
b) \12345.msg
\
true
\12345.msg
false
false
c) 12345.msg
\
true
12345.msg
false
false
d) \12345.msg
\
true
12345.msg
false
false
correct answer is d)
Q24) If in question no 41 the line
File f1 = new File("\\12345.msg"); is replaced with File f1 = new File("12345.msg");
What will be the output
a) 12345.msg
\
true
12345.msg
true
true
b) 12345.msg
null
true
12345.msg
true
true
c) 12345.msg
null
false
12345.msg
true
true
d) \12345.msg
\
true
12345.msg
false
false
Correct answer is c)
Public static void main( String args[])
{
Int x ;
X=5 ;
{
Int y =6;
System.out.print(x +””+ y);
}
System.out.print(x +”” + y);
}
}
What will be output
a) 5656
b) 565
c) Runtime error
d) Compilation Error
Ans -d
Q26)Which one is the valid declaritation of a boolean
a) boolean b1= 1;
b) boolean b2=’flase’;
c) boolean b3= ‘true’
d) boolean b4= false
and- d
Q27)class array_output {
public static void main( String args[]){char array_variable [] = new char[10];
for ( int I =0; i<10 i="" nbsp="" o:p="">10>
{
array_variable [i]= ‘I’ ;
System.out.print(array_variable[i])
}
}
28 ) Class booleanoperator {
Public static void main (String args[]){
boolean var1 = true ;
boolean var2=false ;
System.out.println(var1 & var2);
}
}
29) What does exception arises in the code sequence ?
a)Run Timeb) Complie Time
c) Can not occure ant time
d) None of these
And - a
30) Which of the these keyword can be use for catch exception thrown rational manner
a) try
b) finally
c) thrown
d) catch
Ans- d catch
31) Which of these interface is not part of the java collection framework
a)Listb)Set
c) SortedMap
d) SortedList
And – d
32) which of the interface must contain the order of the element
a) Set
b) List
c) Array
d) Collection
Ans- a Set33 )which of these classes are not part of the collection frame work
a) Maps
b) Array
c) Stack
d) Queue
Ans- d queue34)which of the classes is super class of the all classes
a) Math
b) Process
c) System
d) Object
catch
a) Error
b) Exception
c) Runtime Exception ;
d) All above ;
Ans - a Error ;
a) i I I I i
b) 01234
c) Ijklm
d) None of these
Ans – a
37))What will be the output
a) 0
b) 1
c) Ture
d) False
Ans - d false
Java Screen Test Questions For Interview.
Reviewed by Mukesh Jha
on
2:35 AM
Rating:
No comments:
Add your comment