Answers to Questions and Exercises: Java Fundamentals

1. In the Java programming language, all source code is first written in plain text files ending with the ______ extension.

  1. .javac
  2. .java
  3. .class
  4. .txt

Answer : b

 

2. The Java compiler generates

  1. machine code
  2. source code
  3. byte code
  4. HTML

Answer : c

 

3. JVM stands for

  1. Java Variable Machine
  2. Java Variable Method
  3. Java Virtual Method
  4. Java Virtual Machine

Answer : d

 

4. Write the statement to

  1. compile HelloWorldApp application
  2. Run the program

Answer :

i. javac HelloWorldApp.java

ii. java HelloWorldApp

 

5. Write the signature of the main method.

Answer :

public static void main(String[] args)

 

6. These characters mark the beginning of a single-line comment.

  1. //
  2. /*
  3. */
  4. /**

Answer : a

 

7. Which of the following are not valid assignment statements?

  1. sum = 9;
  2. 72 = cost;
  3. price = 129
  4. ans = 'y'

Answer : b

 

8. Which of the following are valid Java identifiers?

  1. myFirstProgram
  2. 1stProgram
  3. Program1
  4. David'sFirstProgram
  5. First Program
  6. FIRST_PROGRAM

Answer : a, c, f

 

9. This is a named storage location in the computer's memory.

  1. class
  2. keyword
  3. variable
  4. operator

Answer : c

 

10.This keyword is used to declare a named constant.

  1. constant
  2. namedConstant
  3. final
  4. concrete

Answer : c

 

11. Which of the following is not primitive data type?

  1. String
  2. double
  3. boolean
  4. int

Answer : a

 

12. Write Java statements that accomplish the following.

  1. Declare int variables a and b.
  2. Initialize an int variable x to 10 and a char variable ch to 'y'.
  3. Declare and initialize a double variable payRate to 12.50.
  4. Declare a boolean variable ans and set the value of ans to true.

Answer :

  1. int a, b;
  2. int x =10;
        char ch = 'y';
  3. double payRate = 12.50;
  4. boolean ans = true;

 

13. Write the output of the following expressions.

  1. System.out.println(13 / 4);
  2. System.out.println(2 + 12 / 4);
  3. System.out.println(21 % 5);
  4. System.out.println(3 - 5 % 7);
  5. System.out.println(17.0 / 4);
  6. System.out.println(8 - 5 * 2.0);
  7. System.out.println(14 + 5 % 2 - 3);
  8. System.out.println(15.0 + 3 / 2);

Answer :

  1. 3
  2. 5
  3. 1
  4. -2
  5. 4.25
  6. -2.0
  7. 12
  8. 16.0

 

14. What is the value of each variable after the last statement executes?

int a, b, c;
double x, y;
a = 17;
b = 15;
a = a + b / 4;
c = a % 3 + 4;
x = 17 / 3 + 6.5;
y = a / 4.0 + 15 % 4 - 3.5;

Answer :

a = 20
b = 15
c = 6
x = 11.5
y = 4.5

 

15. Suppose x, y, and sum are int variables and z is a double variable. What value is assigned to sum variable after statement executes? Suppose x = 3, y = 5, and z = 14.1.

sum = x + y + (int) z;

Answer : 22


16. Write equivalent statements using combined assignment for the following, if possible.

  1. x = 2 * x;
  2. x = x + y - 2;
  3. sum = sum + num;
  4. y = y / (x + 5);

Answer :

  1. x *= 2;
  2. x += y - 2;
  3. sum += num;
  4. y /= (x+5)

 

17. Change the SampleProgram.java program so that it displays Programming is fun! instead of Hello World!.

class SampleProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}

Answer :

class SampleProgram
{
    public static void main(String[] args)
    {
        System.out.println("Programming is fun!");
    }
}

 

18. The FixProgram.java has some errors. Fix the errors so that the program successfully compiles and runs.

class FixProgram
{
    public static void main(String[] args)
    {
        System.out.println('Hello World!')
    }
}

Answer :


class FixProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!"); 
    }
}