Question:
int counter;
for(counter = 0; counter <= 9; counter = counter + 3){
System.out.println(counter);
}
Answer: The display is:
0
3
6
9
Question: If x has the value 0, then what is the value of the expression?
if(x > 0){
System.out.println(“Positive”);
}else {
System.out.println(“Not positive”);
}
Answer: Not positive
Question: What is true of a field/attribute? (Check ALL the right answers)
Answer:
it is declared outside a method (after the class header)
They are automatically initialized to a default value
it can be called in the whole class
Question: Select the key word we are using in our Java programs to create an object/instance in memory.
Answer: new
Question:
What will the following snippet display when average = 95?
if (!(average > 70)) {
System.out.println(“Low Score”);
}else if (!(average < 70)){
System.out.println(“High Score”);
}
Answer: High Score
Question:
Given the following code fragment, what will display?
int num1 = 2;
int num2 = 3;
if (num1 = num2 ){
System.out.println(“I am if block”);
}else{
System.out.println(“I am else block”);
}
Answer: nothing will display, there is an error in the code
Question:
Fill in the blank in the following For loop to get the output as:
1
6
11
16
21
int counter = 0;
int end = 25;
for(counter = 1; counter <= end; ________){
System.out.println(counter);
}
Answer: counter = counter + 5
Question: Which access specifier is commonly used to declare class’s methods?
Answer: public
Question:
After the following lines are executed, what is the value of num1?
int num1, num2, num3;
num1 = 30;
num2 = 35;
num3 = 12;
if(num1 == num2) {
num1 = num1 + num3;
}else{
num1 = num1 + num1;
}
Answer: 60
Question:
Fill in the blank in the following IF ELSE statement to get the output as: 8
int value =2;
if( _____________ ){
System.out.println(value * value * value);
}else
System.out.println(value);
Answer: value == 2
Question:
int score = 1;
int counter = 1;
Scanner scan = new Scanner(System.in);
if(score < 0) {
System.out.println(“No score found”);
}else{
for(counter = 1; counter < 11; counter++){
score = scan.nextInt();
}
}
Examine the Java code written above and determine
Answer:
Are there any loops in this code? = Yes
Are there any decision structures in this code? = Yes
State how many iterations are performed by the loop? = 10
Does this code fragment identifies the total of scores? = No
Will this code fragment not let user input scores? = No
What will be the value of counter when for loop ends? = 11
Question: What is true about constructor? Select all that apply.
Answer: It has no return type, It has same name as the class, It is called by using new keyword, initialize attributes/fields, and takes no or one or more than one parameters.
Question:
Look at the following snippet from Java code:
public class Tester{
public static void main(String args[]){
Wallet myWallet;
myWallet = new Wallet();
myWallet.getDollar();
myWallet.makeTransaction(50);
}
}
Match the terms with names/terms as they appear in code above.
Answer:
Constructor = Wallet()
Method = getDollar()
Method = makeTransaction(double money)
Argument = 50
Reference variable = myWallet
Class = Wallet
Question: All methods must have a return statement in their body.
Answer: False