Test Your Java 7 and Test Your Java 8 are advanced questions. Answers are given for each question at the end of each test. Answering these questions increases your knowledge in Java and helps you a lot when you appear for interviews. Solving these basic questions is your first step for Certification Exams preparation.
- int i = -25;
System.out.println(Math.sqrt(i));
What is the output of the above fraction code ?
a) 5 b) -5 c) a or b d) throws compilation error e) NaN f) none - In case of insufficient memory, while creating an Object with new keyword, C++ returns null.
What Java returns ?
a) null b) does not return any thing c) displays run-time error d) displays compile-time error e ) none - int x = 10;
System.out.println((x > 10) ? 50.0 : 50);
What could be output of the above fragment of code ?
a) does not compile b) throws an exception c) 50.0 d) 50 e) none - int x = 99, y = 100 ;
System.out.println(x % y);
What is the output of the above fraction of code ?
a) 0 b) 99 c) 0.99 d) none - double d = 45;
int i = (int) d;
The above explicit conversion takes place at
a) compile time b) run time c) depends on the circumstance d) none - .
class Numbers { public static void main(String args[]) { byte b = 50; b = b * 2; System.out.println ("b = " + b); } }
The above program compiles error free.
a) True b) False - byte b;
int i = 257;
b = (byte) i;
System.out.println(“b = ” + b ) ;
The output of the above fragment of code is
a) b = 257 b) b = 256 c) b = 1 d) 0 e) none - int x[] = new int[] { 10, 20, 30 };
Arrays can also be created and intialized as in the above statement.
a) True b) False - .
public class ZeroValue { public static void main (String args[]) { Integer x[] = new Integer[3]; System.out.println(x[0]); } }
What is the output of the above println statemnt ?
a) 0 b) 1 c) null d) does not compile e) throws exception f ) none - .
class ArrayArgument { void display(int arrays[]) { System.out.println(arrays[0] += 2); } public static void main(String args[]) { int intarray[] = { 5, 10, 15 }; ArrayArgument arrarg = new ArrayArgument(); arrarg.display(intarray); System.out.println(intarray[0]); } }
The above program
a) both println statements prints the same value 7 b) one println prints 7 and the other 5 c) does not compile d) compiles but throws an exception e) none - .
public static void main(String args[]) { int min = 0; if(min != 0) System.out.println(min); else return; System.out.println("end of code"); }
What is the output of the above fraction of code ?
a) end of code b) compiles but no output c) does not compile as else statement returns something in a void main method d) throws exception e) none - .
class StaticMethods { static { System.out.println("only static no method"); } public static void main(String args[]) { } }
What is the output of the above program ?
a) only static no method b) compiles but no output as no instance is created c) does not compile as static keyword is not followed by any method name d) none - .
public class MainTest { public static void main() { System.out.println ("SCJP Exam"); } }
What is the output of the above program ?
a) SCJP Exam b) displays compile-time error message as main method is not as per syntax c) compiles but throws an exception at run time d) none - .
class Numbers { public static void main(String args []) { return ; } }
The above class
a) compiles successfully and does not print any thing b) does not compile as there is no access specifier like public in the class declaration c) does not compile as return is not carrying any value d) b and c e) none - The Cloneable interface does not contain any methods.
a) True b) False - Special characters like +, -, *, % , \n etc. should not be used in a String literal.
a) True b) False - String s1 = “SRIRAMA”;
String s2 = “RAMA”;
String s3 = s1 – s2;
System.out.println(s3);
What is the output of the above fraction of code ?
a) SRI b) SRIRAMA c) RAMA d) does not compile e) throws exception f) none - charAt(int) method is included in class
a) String b) StringBuffer c) Object d) a and b e) none - byte charArray[] = { ‘S’,’C’,’J’,’P’ };
System.out.println(charArray[0]);
The above code:
a) prints S b) prints 83 c) compilation error d) raises an exception e) none - StringTokenizer class belongs to the package
a) java.lang b) java.util c) java.io d) java.string e) none - toString( ) method is a member of
a) Object b) Thread c) String d) a, b and c e) a and c f) none - The getClass() method is defined in class
a) Object b) File c) Class d) none - Void is the wrapper implementation of void.
a) True b) False - System.out.println(Math.sin(x));
In the above statement, the value of x should be in
a) degrees b) radians c) none - .
class Test { Test(int qty, double rate) { System.out.println( " Bill amount: " + qty * rate); } public static void main(String args[]) { Test t1 = new Test(); Test t2 = new Test(20, 5.5); } }
The above code
a) prints Bill amount: 110.0 b) raises a compilation error as default constructor is not defined c) raises an exception d) system hangs e) none
ANSWERS
1. e | 2. c | 3. c | 4. b | 5. a |
6. b | 7. c | 8. a | 9. c | 10. a |
11. b | 12. a | 13. c | 14. a | 15. a |
16. b | 17. d | 18. d | 19. b | 20. b |
21. d | 22. a | 23. a | 24. b | 25. b |
Correct Answers |
---|
1 to 5 : Poor |
6 t0 10: Below average |
11 t0 15: Average |
16 to 20: Good |
Above 20: Extraordinary |
Pass your comments and suggestions for the improvement of this "Test Your Java 7".
Question 3 has an extra parentheses. I believe this is a typo given the answer isn’t fail compilation.
System.out.println((x > 10) ? 50.0 : 50));
Yes, it is corrected. Thank you.
can you explain it sir please, I couldn’t understand it
What is your doubt in Test Your Java 7.
System.out.println((x > 10) ? 50.0 : 50));
it’s showing output as
50.0
The statement (x>10) gives false, so the output will be 50.
But how it is 50.0
sir, please explain
Thank you..
For comparison, type casting is done. 50 int becomes double 50.0
Answer for Question 21 of test 7 seems to be wrong. toString() is defined in Thread class.Please clarify
toString() is defined in Object class and overridden by many Java API classes like Thread etc.