Tampilkan postingan dengan label code. Tampilkan semua postingan
Tampilkan postingan dengan label code. Tampilkan semua postingan

Senin, 20 November 2017

Java fundamental section 6

1. The following array declaration is valid. True or false?

int[] y = new int[5];
True (*)

2. double array[] = new double[8];

After execution of this statement, which of the following are true?
array.length is 8 (*)

3. The following array declaration is valid:

int[] y = new int[5];
True (*)

4. The following creates a reference in memory named z that can refer to seven different doubles via an index. True or false?

double z[] = new double[7];
True (*)

5. The following segment of code initializes a 2 dimensional array of primitive data types. True or false?

double[][] a=new double[4][5];
True (*)

6. Which of the following statements print every element of the one dimensional array prices to the screen?
for(int i=0; i < prices.length; i++){System.out.println(prices[i]);} (*)

7. The following segment of code initializes a 2 dimensional array of references. True or false?

String[][] array={{"a", "b", "C"},{"a", "b", "c"}};
True (*)

8. Which of the following statements adds 5 to every element of the one dimensional array prices and then prints it to the screen?

for(int i=0;i<prices.length;i++)
System.out.println(prices[i]+5); (*)

9. What is the output of the following segment of code if the command line arguments are "a b c d e f"?


6 (*)

10. The following array declaration is valid. True or false?
int k[] = new int[10];
True (*)

11. If an exception is thrown by a method, where can the catch for the exception be?
The catch can be in the method that threw the exception or in any other method that called the method that threw the exception. (*)

12. Which of the following could be a reason to throw an exception?
To eliminate exceptions from disrupting your program. (*)

13. Choose the best response to this statement: An error can be handled by throwing it and catching it just like an exception.
False. An error is much more severe than an exception and cannot be dealt with adequately in a program. (*)

14. A computer company has one million dollars to give as a bonus to the employees, and they wish to distribute it evenly amongst them.

The company writes a program to calculate the amount each employee receives, given the number of employees.

Unfortunately, the employees all went on strike before they heard about the bonus. This means that the company has zero employees.

What will happen to the program if the company enters 0 into the employment number?
(Choose all correct answers)

An exception will occur because it is not possible to divide by zero. (*)

15.  What are exceptions used for in Java?
Correcting mistakes made in your code and handling extraordinary cases. (*)


1.  Which line of code shows the correct way to throw an exception?
throw new Exception("Array index is out of bounds"); (*)

2. It is possible to throw and catch a second exception inside a catch block of code. True or false?
True (*)

3. What exception message indicates that a variable may have been mispelled somewhere in the program?
variableName cannot be resolved to a variable (*)

4. Choose the best response to this statement: An error can be handled by throwing it and catching it just like an exception.
False. An error is much more severe than an exception and cannot be dealt with adequately in a program. (*)

5. A computer company has one million dollars to give as a bonus to the employees, and they wish to distribute it evenly amongst them.

The company writes a program to calculate the amount each employee receives, given the number of employees.

Unfortunately, the employees all went on strike before they heard about the bonus. This means that the company has zero employees.

What will happen to the program if the company enters 0 into the employment number?
(Choose all correct answers)
An exception will occur because it is not possible to divide by zero. (*)

6. Which of the following declares and initializes a two dimensional array that can hold 6 Object reference types?
Object[][] array=new Object[2][3]; (*)

7. What is the Output of the following segment of code?

666666 (*)

8. The following creates a reference in memory named k that can refer to six different integers via an index. True or false?

int k[]= int[6];
False (*)

9. Which of the following declares a one dimensional array name scores of type int that can hold 14 values?

int[] scores=new int[14]; (*)

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

int num[]={9,8,7,6,5,4,3,2,1};
for(int i=0;i<9;i=i+3)
System.out.print(num[i]);
963 (*)

11. The following creates a reference in memory named z that can refer to seven different doubles via an index. True or false?

double z[] = new double[7];
True (*)

12. The following array declaration is valid:

int[] y = new int[5];
True (*)

13. Which of the following declares and initializes a one dimensional array that can hold 5 Object reference types?
Object array=new Object[5]; (*)


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 (*)