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.
- .javac
- .java
- .class
- .txt
2. The Java compiler generates
- machine code
- source code
- byte code
- HTML
3. JVM stands for
- Java Variable Machine
- Java Variable Method
- Java Virtual Method
- Java Virtual Machine
4. Write the statement to
- compile HelloWorldApp application
- Run the program
5. Write the signature of the main method.
6. These characters mark the beginning of a single-line comment.
- //
- /*
- */
- /**
7. Which of the following are not valid assignment statements?
- sum = 9;
- 72 = cost;
- price = 129
- ans = 'y'
8. Which of the following are valid Java identifiers?
- myFirstProgram
- 1stProgram
- Program1
- David'sFirstProgram
- First Program
- FIRST_PROGRAM
9. This is a named storage location in the computer's memory.
- class
- keyword
- variable
- operator
10.This keyword is used to declare a named constant.
- constant
- namedConstant
- final
- concrete
11. Which of the following is not primitive data type?
- String
- double
- boolean
- int
12. Write Java statements that accomplish the following.
- Declare int variables a and b.
- Initialize an int variable x to 10 and a char variable ch to 'y'.
- Declare and initialize a double variable payRate to 12.50.
- Declare a boolean variable ans and set the value of ans to true.
13. Write the output of the following expressions.
- System.out.println(13 / 4);
- System.out.println(2 + 12 / 4);
- System.out.println(21 % 5);
- System.out.println(3 - 5 % 7);
- System.out.println(17.0 / 4);
- System.out.println(8 - 5 * 2.0);
- System.out.println(14 + 5 % 2 - 3);
- 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;
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.
- x = 2 * x;
- x = x + y - 2;
- sum = sum + num;
- 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!') } }
No comments:
Post a Comment