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.
Test Your Java – 1
Read all the choices carefully, as there may be more than one correct answer, choose all the correct answers for each question.
- ________________________ makes Java platform-independent.
a) JVM b) Java syntax c) Java API d) bytecodes e) none - Java's keywords includes Null.
a) True b) False - Which occupies more number of bits of memory.
a) double b) long c) both 4 b) occupies same d) depends on the value assigned e) none - The object is created with new keyword
a) at compile-time b) at run-time c) depends on the code d ) none - int x = 0, y = 0 , z = 0 ;
x = (+ + x + y – – ) * z + + ;
What is the value of " x " after execution ?
a) – 2 b) – 1 c) 0 d ) 1 e) 2 - int 4thhouse = 1234 ;
System.out.println( 4thhouse ) ;
a) 1234 b) displays error as the value assigned is more than the range of integer c) displays error as coding is not as per Java rules e) none - int ++a = 100 ; System.out.println( ++a ) ;
What is the output of the above fraction of code ?
a) 100 b) displays error as + + a is not enclosed in double quotes in println statement c) compiler displays error as + + a is not a valid identifier d) none - Integer.parseInt( ) method is used to convert an integer value to its string form.
a) True b) False - Java supports unsigned data types.
a) True b) False - One way of implementing data protection is declaring instance variables as private and methods as public.
a) True b) False - How many primitive data types Java defines ?
a) 6 b) 8 c) 10 d) more than 10 e) none - The variables declared in a class for the use of all methods of the class are called
a) reference variables b) objects c) instance variables d ) none - double STATIC = 2.5 ;
System.out.println( STATIC ) ;
The above fraction of code
a) prints 2.5 b) raises an error as STATIC is used as a variable which is a keyword c) raises an exception e) none - What is the range of data type int ?
a) – ( 2^16 ) to ( 2^16 ) –1 b) – ( 2^15 ) to ( 2^15 ) –1 c) – ( 2^31 ) to ( 2^31 ) –1 d) – ( 2^32 ) to ( 2^32 ) –1 e) depends on the operating system on which Java is working f) none - int Integer = 34 ;
char String = 'S' ;
System.out.println( Integer ) ;
System.out.println( String ) ;
What would be the output of the above fraction of code ?
a) does not compile as Integer and String are API class names b) throws exception c) 34 d) S e) c and d f) none - char k = 'A' ;
System.out.println( k * k ) ;
The above program raises a compilation error as arithmetic operations are not possible on characters. a) true b) false - System.out.println( Math.floor( Math.random( ) ) ) ;
The above statement always prints 0.0
a) True b) False - int x = 99 , y = 100 ;
System.out.println( x / y ) ;
What is the output of the above fraction of code ?
a) does not compile b) 0 c) 0.99 d) none - boolean b = true ;
int i = ( int ) b ;
System.out.println( i ) ;
What is the output of the above fraction of code ?
a) 116 ( the ASCII value of character t ) b) 98 ( the ASCII value of character b ) c) does not compile d) throws exception e) none - System.out.println(25/4.0);
System.out.println(25.0/4.0);
System.out.println(25.0/4);
The output of the above three println statements is same result.
a) True b) False -
class Num { Num(double x ) { System.out.println( x ) ; } } class Numbers extends Num { public static void main(String[] args) { Num num = new Num( 2 ) ; } }
What is the output of the above program ?
a) 0 b) 2.0 c) error as mismatch between constructors d) none - byte b = 50 ;
b = b * 2 ;
System.out.println( “ b = “ + b ) ;
The above fraction of code prints b = 100.
a) True b) False -
public class Numbers { static int x = 10 ; public static void main(String[] a) { Numbers num = new Numbers( ) ; Numbers num1 = new Numbers( ) ; num.x += 1 ; System.out.println( num.x + num1.x ) ; } }
What is the output of the above program ?
a) 20 b) 21 c) 22 d) does not compile e) throws exception e) none ( ) - Constructors can be declared final, if needed perhaps which you must have not tried.
a) True b) False - final methods cannot be overridden but overloaded ?
a) True b) False
21.
23.
ANSWERS
1. d | 2. b | 3. c | 4. b | 5. c |
6. c | 7. c | 8. b | 9. b | 10. a |
11. b | 12. c | 13. a | 14. c | 15. e |
16. b | 17. a | 18. b | 19. c | 20. a |
21. c | 22. b | 23. c | 24. b | 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 1".
Sir,Is Java pure object oriented as my sir told us,n if not then please give me reason for it.or any blog you have related to it.
It is pure object-oriented.
sir , i have some knowledgr about java Lopgger. Could you please tell me what is log4j.
It is logging mechanism in a Java application supported many tools IDE like Eclipse, NetBeans etc.
hello sir…
will u explain test 1 question 21…?
Subclass overload (or default also) constructor will super class default constructor. But super class does not have default constructor and is also not created by the system as the constructor is overloaded.
See this link:
http://way2java.com/oops-concepts/constructors-and-constructor-overloading/
que 23.
how 22..? it should be 21..!!
It is static variable. All object will share the same location. Read this link:
http://way2java.com/oops-concepts/static-keyword-%e2%80%93-philosophy/
sir
why does System.out.println(99/100); prints 0? any specific reason? actually it is .99
It is integer division. Make it either 99.0/100 or 99/100.0, you get yours.
20 is bad, too. Output includes strings and they ARE different.
9 is bad, because char IS unsigned.
9) Java does not support “unsigned” datatypes for that matter “unsigned” is not keyword of Java. “char” is implicitly unsigned.
Java does support pointers and it does not mean the designers should not use pointers in developing the language.
20) The output is same answer of division quotient.
Remember these questions for very beginners. If you think of Strings and String pools, I will modify the question for you and pass your comments.
Than for 9 the question should sound as “Is a word ‘unsigned’ used for some Java types definition?”
As for 20, you haven’t understood. The outputs include strings “20/4.0” and “20.0/4” And they ARE different. But, as I see, you have changed them already. Thank you.
12 is wrong. Instance variables are not seen from all methods of the class. For example they are not seen from static methods.
public class Demo
{
int x = 10;
public static void main( String args[])
{
Demo d1 = new Demo();
System.out.println(d1.x);
}
}
main() method is static and still you are able to access x. The difference is you require an object to call from static methods.
Because you have created the instance just here. You can’t see it from another static method.
The variables seen from anywhere in the class are called Static Class attributes or Static Class Fields or Static Class Variables.
Respected sir
Recently i attended a interview where people asked some questions like Difference between ByteArrayOutStream and BufferedOutputStream and where do we use them ?
so please post a detailed explanation to this to give us some in depth idea on this
Thanks in advance
Thanks and Regards
Venkata Naveen
See this may help you.
1. http://way2java.com/io/java-io-streams-overview/
2. http://way2java.com/io/byte-streams-and-character-streams/
Performance aspect of BufferedInputStream over FileInputStream:
1. http://way2java.com/io/semantics-of-file-copying/
2. http://way2java.com/io/bufferedinputstream-and-bufferedoutputstream/
http://way2java.com/io/semantics-of-file-copying/
21 answer is b not a
In Test Your Java – 1, the answer for 21 question is C.
interface Side {
String getSide();
}
class Head implements Side {
public String getSide()
{
return “Head “; }
}
class Tail implements Side {
public String getSide()
{
return “Tail “; }
}
class Coin {
public static void overload(Head side) { System.out.print(side.getSide()); }
public static void overload(Tail side) { System.out.print(side.getSide()); }
public static void overload(Side side) { System.out.print(“Side “); }
public static void overload(Object side) { System.out.print(“Object “); }
public static void main(String []args) {
Side firstAttempt = new Head();
Tail secondAttempt = new Tail();
overload(firstAttempt);
overload((Object)firstAttempt);
overload(secondAttempt);
overload((Side)secondAttempt);
}
}
sir can u please explain this program .In main,how can overload(firstAttempt); wil call overload(Side side) method?and even explain when we call
overload((Object)firstAttempt);
overload(secondAttempt);
overload((Side)secondAttempt);,which methods are called?
class Base {
public static void foo(Base bObj) {
System.out.println(“In Base.foo()”);
bObj.bar();
}
public void bar() {
System.out.println(“In Base.bar()”);
}
}
class Derived extends Base {
public static void foo(Base bObj) {
System.out.println(“In Derived.foo()”);
bObj.bar();
}
public void bar() {
System.out.println(“In Derived.bar()”);
}
}
class OverrideTest {
public static void main(String []args) {
Base bObj = new Derived();
bObj.foo(bObj);
}
}
sir! can you please explain how can bobj object call foo() method of base class?as sub class obj is assigned to super class object,the object should call sub class overidden method as per rules!
int ++a = 100 ; System.out.println( ++a ) ;
What is the output of the above fraction of code ?
a) 100
b) displays error as + + a is not enclosed in double quotes in println statement
c) compiler displays error as + + a is not a valid identifier
d) none
Answer: C
Sir Explain this.
In int ++a = 100, ++a is an identifier. An identifier should not have any special characters, by rule.
byte b = 50 ;
b = b * 2 ;
System.out.println( “ b = “ + b ) ;
The above fraction of code prints b = 100.
a) True b) False
Answer is false.
Sir, Please explain this.
In runtime, b*2 when multiplied results in int value. An int value cannot be assigned to byte value implicitly. For this reason, it is compilation error. To work correctly, do as following explicit conversion:
b = (byte)(b*2);
char k = ‘A’ ;
System.out.println( k * k ) ;
The above program raises a compilation error as arithmetic operations are not possible on characters. a) true b) false
answer 9409,,, there is no compilation error ,it prints some integer value
tel me sir why it is taken integer value?
At runtime, A value is converted into its ASCII integer of 65 and then multiplied.
int Integer = 34 ;
char String = ‘S’ ;
System.out.println( Integer ) ;
System.out.println( String ) ;
What would be the output of the above fraction of code ?
a) does not compile as Integer and String are API class names b) throws exception c) 34 d) S e) c and d f) none
how it is working String is a Data Type how ur using Integer and String as a Variable i cant believe , please explain me in detail sir
Java API classes are not keywords. So, by principle, it is possible.
Java API classes (predefined class of Java) are not keywords. They can be used as variables; but we do not due to confusion and later problems arise.
in ques 21 there is error that it is not finding the constructor if you perform this in notepad in wind 7 becouse parametrized constructor are never inherited but if you perform that in eclipse it is showing the result ….. sir plz tell me why its happening
output for 21 is B and not C as mentioned in the answer
Just write a program and compile, you will come to know.
To have smooth compilation, add the constructor Num(){ } this in Num class.
as the super class is having parameterised constructor and object of Num class is created using parameterised constructor what is the need of default constructor?
JVM requires else it is compilation error.
Question num 23 answer is c not b..
when we declare static variables only one copy per class is available.
It is given as c only and not b in Test Your Java – I
please explain 16,22,24.
What is 16, 22 and 24 ? Are they from Test series? Then give the series no.