Total 8 tests are available under "Test Your Java" series. Test-1, Test-2, Test-3, Test-4, Test-5, Test-6 are basic level questions and Test-7, Test-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 SCJP preparation.
-
class Numbers { public int display(int x, int y) { return ("The sum of x and y is " + x+y); } public static void main(String args[]) { Numbers num = new Numbers(); System.out.println(num.display(4,5)); } }
What is the output of the above program ?
a) The sum of x and y is 9 b) The sum of x and y is 45 c) does not compile d) none -
class WhatOutput { public void display(int x, double y) { System.out.println(x+y); } public double display(int p, double q) { return (p+q); } public static void main(String StringArray[]) { WhatOutput wo = new WhatOutput(); wo.display(4, 5.0); System.out.println(wo.display(4, 5.0)); } }
The output of the above program is 9.0 and 9.0.
a) True b) False -
class staticMethods { static void display() { System.out.println("display"); } static { System.out.println("only static no method"); } public static void main(String s[]) { display(); } }
What is the output of the above program ?
a) display b) only static no method and display c) display and only static no method d) does not compile due to error in third line e) none - Overloaded methods must have the same return types.
a) True b) False -
public class Room { public static void main(String args[]) { int height=10 , width=10, length=20; System.out.println("Volume is " + (width*height*length)); } }
The above program creates an object by name Room and displays its volume as 2000.
a) True b) False - The implicit return type of a constructor is
a) void b) depends upon the called method c) a class object in which it is defined d) none -
class Numbers { public static void main(String args[]) { int a=20, b=10; if((a < b) && (b++ < 25)) System.out.println("This is any language logic"); System.out.println(b); } }
What is the output of the above program ?
a) 12 b) 11 c) 10 d) program does not compile e) throws exception f) none - Both switch and if tests for boolean type.
a) True b) False - In Java every method need not be associated with an object.
a) True b) False - The finalize() method is called just prior to
a) an object, variable or method goes out of scope.
b) an object or variable goes out of scope.
c) a variable goes out of scope d) before garbage collection e) none - All classes in a source file should contain main method.
a) True b) False - The main method should be static for the reason
a) it can be accessed easily by the class loader.
b) it can be accessed by every method or variable without any hindrance.
c) it can be executed without creating any instance of the class. d) none -
class Weather { static boolean isRaining; public static void main(String args[]) { System.out.print(isRaining); } }
The above program
a) prints true b) prints false c) does not compile as boolean is not initialized d) does not compile boolean can never be static e) c and d f) none -
int i; for(i = 1; i < 6; i++) { if (i > 3) continue ; } System.out.println(i);
What is the output of the above fraction of code ?
a) 2 b) 3 c) 4 d) 5 e) 6 f) does not compile g) none - Constructors and methods of a class can be inherited.
a) True b) False - Float f1 = new Float("3.2");
float f = f1.floatValue();
System.out.println(f);
The above statements raises a compilation error as 3.2 is not mentioned as 3.2f.
State a) True b) False - I can instantiate a class which implement an interface.
a) True b) False - An abstract class should have methods all declared abstract.
a) True b) False - An interface can be extended from another interface.
a) True b) False -
class XXX { void show() { System.out.println("XXX"); } } class YYY extends XXX { void show() { super(); System.out.println("YYY") ; } public static void main(String args[]) { new YYY().show(); } }
What is the output of the above program ?
a) XXX b) YYY c) a and b d) does not compile e) throws exception f) none -
public static void main(String args[]) { public int firstNumber = 1; private double secondNumber = 1.0; System.out.println(firstNumber + secondNumber); }
The above code prints 2.0. State a) true b) false
- int first /* house */ Number = 1;
System.out.println(firstNumber);
The above code prints 1. State a) true b) false -
switch(1) { case 1: System.out.println("SNRao"); break; case 1: System.out.println("Sridhar"); break; }
Writing same case statement two times is a compilation error.
State a) true b) false -
class ForLoop { public static void main(String args[]) { for(int i = 0, int j = 0; i < 3; i++, j++) System.out.println(i + j); } }
What is the output of the above program ?
a) 0, 2, 4 b) 0, 1, 2 c) does not compile d) throws exception e) none -
class GuessWhat { public static void main(String args[]) { int min = 0; min(10, 20, min); System.out.println(min); } public static void min(int number1, int number2, int min) { if(number1 > number2) min = number1; else min = number2; } }
What is the output of the above program ?
a) 0 b) 10 c) 20 d) does not compile as the compiler can not differentiate between min variable and min method e) throws exception due to min variable in min method f) none
1.
2.
3.
5.
7.
13.
14.
20.
21.
23.
24.
25.
ANSWERS
1. c | 2. b | 3. b | 4. b | 5. b |
6. c | 7. c | 8. b | 9. a | 10. d |
11. b | 12. c | 13. b | 14. e | 15. b |
16. b | 17. a | 18. b | 19. a | 20. d |
21. b | 22. b | 23. a | 24. c | 25. a |
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 2".
23 question ans : Wrong
its not true
its false. and Compile time error
Read the answer statement once again. It says it is compilation error. So, it is true.
Sir can you please confirm question 4?overloaded method must have same return type.isnt that true?
Overloaded method can have any return type as compiler does not bother about return type.
sir can you explain about 15 and 20?
because the constructor can be inherited from base class like methods.
Constructors cannot be called like methods with objects. To call a constructor, it is necessary to create an object.
super() is used with constructors but not with methods.
Thank you sir.
it would have been ok if it was like super.show();
sir please explain 25th question of test series-2!..how the output is ‘0’??
min = number1;
Here min is local variable passed as parameter to method void min() and not instance variable min. For this reason, the instance variable does not change.
for 25 Q.sir
how can int min=0 be a instance variable as it is declared inside program?
min = number1
Local variable is getting changed and not the int min = 0;
for 25
how can int min=0 be a instance variable when it is declared inside the main function
In the whole question, there is no instance variable.
Answer for Q 16 is A — Error in the Answer
Understand the question leisurely. The code does not raise error (the question says, raises error) and that is the answer is false.
The Float constructor is overloaded that takes a string and float value as parameters.
Answer for Question no 6 Test 2 should be D , because constructors do not have a return type
Internally constructor returns a class object in which it is defined (for this reason we do not write void).
5. Object is required .. s definetly.
Basically comilation error but program prints 2000 how it is possible?
give me your explaination sir…
Here, Room object is not created. It is simply a multiplication of three values.
class staticMethods
{
static void display()
{
System.out.println(“display”);
}
static
{
System.out.println(“only static no method”);
}
public static void main(String s[])
{
display();
}
}
please explain this program… sir
static blocks are executed before main() method.
static blocks are called before main() is executed.
public static void main(String args[])
{
int min = 0;
min(10, 20, min);
System.out.println(min);
}
Parameter min does not change the variable int min=0.
5. b wrong answer , actually it is true(a)
23. a wrong answer, it is b
5th question: My answer is correct. Variables like width etc. are local variables. Here, no object is created and it is simply a mathematical calculation. Had the variables are instance variables, then you have to create Room object and call the variables.
23rd question: Writing two times case 1: is a compilation error.
Sir, it is compiling and gave output as “Volume is 2000”
Here, a Room object is not created like Room r1 = new Room(). It is simply a multiplication of 3 instance variables width*height*length.
5. Here, no Room object is created. It is simply a multiplication of three variables.
23. Try in a program. You find it is a compilation error. So, a is correct.