Java Exercices 1 Answers
Java Exercices 1 Answers
A. boolean b1 = 0;
B. boolean b2 = 'false';
C. boolean b3 = false;
D. boolean b4 = Boolean.false();
E. boolean b5 = no;
Explanation:
2.
switch(x)
{
default:
System.out.println("Hello");
}
Which two are acceptable types for x?
1. byte
2. long
3. char
4. float
5. double
6. Long
A. 1 and 3 B. 2 and 4
C. 3 and 5 D. 4 and 6
1
Explanation:
Switch statements are based on integer expressions and since both bytes
and chars can implicitly be widened to an integer, these can also be used.
Also shorts can be used.
3.
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
Which statement is true?
A. Compilation fails.
"odd" will be output for odd values of x, and "even" for even
D.
values.
Explanation:
The compiler will complain because of incompatible types (line 4), the if
expects a boolean but it gets an integer.
2
4. What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
A. true
B. false
C. Compilation fails
Explanation:
The code will not compile because in line 7, the line will work only if we
use (x==y)in the line. The == operator compares values to produce a
boolean, whereas the =operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as
compiling. If we corrected this code, the output would be false.
[A] javac
[B] java
3
[C] javad
[D] .javadoc
[A] Output
[B] Bytecode
4
[C] Error
[A] polymorphism
[B] encapsulation
[C] inheritance
Explanation: Inheritance is the process by which one class can inherit the
properties from another class.
5
10. The extension name of a Java source code file is ?
[A] .java
[B] .obj
[C] .class
[D] .exe