JAVA CH 5&6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

If a boolean variable named isValid is used a Java if condition, it must always be used in the following way to see if the condition is true: if (isValid == true) {... (T or F)

False )It can be used that way, but can also be used like this: if (isValid) {... )

The following code: Random r = new Random(); int x = r.nextInt(4) + 1; will result in x being assigned a value between 1 and 5. (T or F)

False (It will be between 1 and 4)

The input cursor is only able to skip one whitespace character when moving from one input token to the next. (T or F)

False (The input cursor will skip all whitespace characters between input tokens, regardless of how many there are.)

The following code: Random r = new Random(); int x = r.nextInt(12) + 5; will result in x being assigned a value between 5 and 17. (T or F)

False (The range of values will be 5 to 16.)

The Random class in Java does not require an import statement in order to use it in a Java program. (T or F)

False (You must import java.util.*; to use Random.)

Given the following Java code: public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int num, total = 0; int count = 0; do {System.out.print("Enter a number: "); num = keyboard.nextInt(); if (num < 5) num = num * 2; elsenum = num - 1; total = total + num; count++;} while (count <= 3); System.out.println("Total = " + total);} // end main This loop will execute 3 times. (T or F)

False (it will actually execute 4 times)

The input cursor can move both forward and backward while reading data from a file. (T or F)

False (the input cursor can only move forward.)

What is the proper way to code the following statement in Java?if (3 < x < 8)

If ( 3 < x && x <8)

What is the name of the Java method that determines if a file exists on the system and can be read for input?

canRead()

For a __________ loop, a semi-colon must come after the parentheses that contain the boolean expression.

dowhile

What is the name of the Java method that determines whether or not a file exists on the system?

exists()

What is the proper way to code the following statement in Java? if (x is 1, 2 or 3)

if (x == 1 || x == 2 || x ==3) <-- correct Answer

What is the name of the Java method that returns the number of characters in a file?

length()

Suppose a Java program uses a file for input. The file is opened in main() through a Scanner object and there are three other methods which have the file's Scanner object passed to them as a parameter. How many of the methods must have throw FileNotFoundException in their header?

main() (Only)

The following code: Random r = new Random(); int x = r.nextInt(8); will result in x being assigned a value between 1 and 8. (T or F)

False (The range of values will be 0 through 7.)

The Java method that returns the full directory path of a file in the system is getAbsolutePath(). (T or F)

True

The input cursor cannot be set back to the beginning of a file once it has been used in a Scanner object. (T or F)

True

When a Scanner object is first created with a file, the input cursor points to the beginning of the file. (T or F)

True

You must put the code throws FileNotFoundException in the header of the main() program when opening files in main() in a Java program. (T or F)

True

Calculate the value of z after the following Java code executes: public static void main(String[] args) { // TODO Auto-generated method stub int x = 5; int y = 6; int z = 0; if ((x <= 7) && (y > 5))z = x + y; else if ((x > 7) || (y < 5)) z = x - y; else if ((x <= 7) && (y < 6)) z = x * y; else z = 1; System.out.println("z = " + z); }

11

Given the following Java code:public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); int num, total = 0; int count = 0; do {System.out.print("Enter a number: "); num = keyboard.nextInt(); if (num < 5) num = num * 2;else num = num - 1; total = total + num;count++;} while (count <= 3); System.out.println("Total = " + total);} // end main Assume the inputs are as follows: 8, 4, 6, 5What is the value of total when the program is finished executing?

24

Given the following Java code: public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); int num, total = 0; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); while (num != -1){ if (num > 5) num = num - 1; else num = num * 2; total = total + num; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt();} // end while System.out.println("Total = " + total);} // end main Assume the inputs are as follows: 10, 4, 8, 5, 2, -1What is the value of total when the program ends?

38

In Java, a Scanner object can be attached to ____________. The console for user imput, a file for file input, an individual String, All of the above.

All of the above

A variable that is used in the loop test of a while loop in Java does not need to be initialized before the while loop is entered.

It must be initialized to some value that allows the loop to start.

A directory path that begins with the name of a directory that is contained in the current directory, rather than the drive letter or root directory, is called a(n) __________ path

Relative

You must create a __________ object if you want to output data to a file in a Java program.

Relative

Given the following Java code: public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int num, total = 0; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); while (num != -1){ if (num > 5) num = num - 1; elsenum = num * 2; total = total + num; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt();} // end while System.out.println("Total = " + total);} If the first input is -1, this loop will never execute. (T or F)

True

What is the simplified negation of the following Java expression after applying De Morgan's Law?while (x >= 15 && x < 35)

While ( x<15 || x>= 35)


Kaugnay na mga set ng pag-aaral

Saunders Pre-Test Mode 75 Questions

View Set

Capitais dos Países da America latina

View Set

A&P Chapter 5: Integumentary System Review

View Set

Review for Biology Quiz of Test #2

View Set

Nutrition Chapter 7 (Proteins) Reading Questions

View Set

Chapter 6: Conducting Survey and Self-Report Research

View Set