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.
- .javac
- .java
- .class
- .txt
Answer : b
2. The Java compiler generates
- machine code
- source code
- byte code
- HTML
Answer : c
3. JVM stands for
- Java Variable Machine
- Java Variable Method
- Java Virtual Method
- Java Virtual Machine
Answer : d
4. Write the statement to
- compile HelloWorldApp application
- 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.
- //
- /*
- */
- /**
Answer : a
7. Which of the following are not valid assignment statements?
- sum = 9;
- 72 = cost;
- price = 129
- ans = 'y'
Answer : b
8. Which of the following are valid Java identifiers?
- myFirstProgram
- 1stProgram
- Program1
- David'sFirstProgram
- First Program
- FIRST_PROGRAM
Answer : a, c, f
9. This is a named storage location in the computer's memory.
- class
- keyword
- variable
- operator
Answer : c
10.This keyword is used to declare a named constant.
- constant
- namedConstant
- final
- concrete
Answer : c
11. Which of the following is not primitive data type?
- String
- double
- boolean
- int
Answer : a
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.
Answer :
- int a, b;
- int x =10;
char ch = 'y'; - double payRate = 12.50;
- boolean ans = 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);
Answer :
- 3
- 5
- 1
- -2
- 4.25
- -2.0
- 12
- 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;
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
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.
- x = 2 * x;
- x = x + y - 2;
- sum = sum + num;
- y = y / (x + 5);
Answer :
- x *= 2;
- x += y - 2;
- sum += num;
- 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!"); } }
No comments:
Post a Comment