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

 

2. The Java compiler generates

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

 

3. JVM stands for

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

 

4. Write the statement to

  1. compile HelloWorldApp application
  2. Run the program

 

5. Write the signature of the main method.

 

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

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

 

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

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

 

8. Which of the following are valid Java identifiers?

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

 

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

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

 

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

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

 

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

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

 

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.

 

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);

 

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;

 

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;

 

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);

 

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!");
    }
}

 

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!')
    }
}