Senin, 20 November 2017

Java Fundamental Section 5

1.  What will print if the following Java code is executed?

if ((5.1 > 4.3 && 6.2 < 8.4) && !(7.2 < 3.5 || 1.2 == 2.1 || 2.2 != 2.25))
System.out.print("TRUE");
else
System.out.print("FALSE");
False (*)

2.  What is the difference between the symbols = and == ?
The = is use to assign values to variables and the == compares values. (*)

3.  Consider that a Scanner has beeLabel
n initialized such that:

Scanner in = new Scanner(System.in);

Which of the following lines of code reads in the userメs input and sets it equal to a new String called input?
String input = in.next(); (*)

4.  In Java, each case seqment of a switch statement requires the keyword break to avoid "falling through".
True (*)

5.  Which of the following could be a reason to use a switch statement in a Java program?
Because it allows the program to run certain segments of code and neglect to run others based on the input given. (*)

6.  Which of the following correctly matches the switch statement keyword to its function?
(Choose all correct answers)
default: signals what code to execute if the input does not match any of the cases (*)

case: signals what code is executed if the user input matches the specified element (*)
switch: identifies what element will be compared to the element of the case statements to find a possible match (*)

7.  The three logic operators in Java are:

&&, ||, ! (*)

8.  The following code fragment properly implements the switch statement. True or false?

default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input");
False (*)

9.  What is a loop?
A set of logic that is repeatedly executed until a certain condition is met. (*)

10.  One advantage to using a while loop over a for loop is that a while loop always has a counter. True or false?
False (*)

11.  The syntax below represents a valid initialization of a for loop counter. True or False?

public class ForLoop {
 public static void main (String args[])
 {
  for (int i=10; i <20; i++)
  {System.out.println("i: "+i); }
 }
}
True (*)

12.  What is one significant difference between a while loop and a do-while loop?
A DO-WHILE loop will always execute the code at least once, even if the conditional statement for the WHILE is never true. A WHILE loop is only executed if the conditional statement is true. (*)

13.  Identify which situation could be an example of a while loop.
All of the above. (*)

14.  Which of the following are types of loops in Java?

(Choose all correct answers)
while (*)
for (*)
do-while (*)

15.  In a for loop, the counter is automatically incremented after each loop iteration. True or False?
False (*)

1.  In an if-else construct, the condition to be evaluated must be contained within parentheses. True or False?
True (*)

2.  Which of the following are relational operators in Java?

(Choose all correct answers)
< (*)
<= (*)
!= (*)

3.  Which of the following correctly initializes an instance of Scanner, called "in", that reads input from the console screen?

Scanner in = new Scanner(System.in); (*)

4.  Which of the following correctly matches the switch statement keyword to its function?
(Choose all correct answers)
default: signals what code to execute if the input does not match any of the cases (*)
switch: identifies what element will be compared to the element of the case statements to find a possible match (*)
case: signals what code is executed if the user input matches the specified element (*)

5.  Which of the two diagrams below illustratethe correct syntax
for variables used in an if-else statement?














Example A(*)

6.  How would you use the ternary operator to rewrite this if statement?

if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms.");
System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)

7.  In an if-else construct the condition to be evaluated must end with a semi-colon. True or false?
False (*)

8.  This keyword is used to instruct specific code when the input for a switch statement that does not match any of the cases.
Default(*)

9.  What is the output of the following code segment?

int num = 7;
while(num >= 0)
{
num -= 3;
}
System.out.println(num);
-2 (*)

10.  What should replace the comment "//your answer here" in the code below if the code is meant to take no action when i % 2 is 0 (in other words when i is even)?

for(int i = 0; i < 10; i++){
if(i%2 == 0)
//your answer here
else
k+=3;
}
Continue (*)

11.  What is one significant difference between a while loop and a do-while loop?
A DO-WHILE loop will always execute the code at least once, even if the conditional statement for the WHILE is never true. A WHILE loop is only executed if the conditional statement is true. (*)

12.  A counter used in a for loop cannot be initialized within the For loop statement. True or False?
False (*)

13.  In a for loop the counter is not automatically incremented after each loop iteration. Code must be written to increment the counter. True or false?
True (*)

14.  One advantage to using a while loop over a for loop is that a while loop always has a counter. True or false?
False (*)

15.  When the for loop condition statement is met the construct is exited. True or false?
False (*)

1 komentar: