0% found this document useful (0 votes)
17 views

Java Exercices 1 Answers

The document contains multiple choice questions about Java concepts. Question 1 asks about valid boolean declarations, with only option C being valid. Question 2 asks about valid data types for a switch statement, with options 1 and 3 (byte and char) being acceptable. Question 3 asks about a compilation error in an if statement, with option A being the answer that compilation fails.

Uploaded by

Vignesh S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Exercices 1 Answers

The document contains multiple choice questions about Java concepts. Question 1 asks about valid boolean declarations, with only option C being valid. Question 2 asks about valid data types for a switch statement, with options 1 and 3 (byte and char) being acceptable. Question 3 asks about a compilation error in an if statement, with option A being the answer that compilation fails.

Uploaded by

Vignesh S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Which one is a valid declaration of a boolean?

A. boolean b1 = 0;

B. boolean b2 = 'false';

C. boolean b3 = false;

D. boolean b4 = Boolean.false();

E. boolean b5 = no;

Explanation:

A boolean can only be assigned the literal true or false.

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.

B. "odd" will always be output.

C. "even" will always be output.

"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

D. An exception is thrown at runtime

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.

5. Which command is used to compile a java program ?

[A] javac

[B] java

3
[C] javad

[D] .javadoc

Explanation: The javac command in Java compiles a program from a


command prompt. It reads a Java source program from a text file and creates
a compiled Java class file. The basic form of the javac command is "javac
filename "

6. What is the full form of JVM

[A] Java Virtual Machine

[B] Java Variable Machine

[C] Java Virtual Mechanism

[D] Java Variable Mechanism

Explanation: A Java virtual machine (JVM) is a virtual machine that can


execute Java bytecode. It is the code execution component of the Java
platform.

7. Which of the following is generated when the source code is successfully


compiled ?

[A] Output

[B] Bytecode

4
[C] Error

[D] None of above

Explanation: Bytecode is the compiled format for Java programs. Once a


Java program has been converted to bytecode, it can be transferred across a
network and executed by Java Virtual Machine (JVM).

8. In java , if you do not give a value to a variable before using it ,______

[A] It will contain a garbage value

[B] It will initialized with zero

[C] Compiler will give an error

[D] None of above

9. Sharing of common information is achieved by the concept of ?

[A] polymorphism

[B] encapsulation

[C] inheritance

[D] none of above

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

11. Who invented JAVA?

[A] James Gosling

[B] Dennis Ritchie

You might also like