Java practice questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

Assuming that variable base has a value of 5 and variable height has a value of 12, what output is generated by the code segment below? if (base >= height / 2) { System.out.print(base); } else { System.out.print(height); } System.out.print(" " + (base * height) / 2);

12 30

Which of the following is NOT a valid number literal of type double?

2.5E35 2503.0 2,503.50 x 2503.5

What value is returned by the method call Math.pow(1 + 2, 3) ?

27.0

What output does this while loop generate? int number = 1; while (number <= 5) { int value = number * 3; System.out.print(value + ", "); number++; }

3, 6, 9, 12, 15,

What is the value of variable result after executing the statements below? int limit = 2; double result = 3.5 + 5 / (6 - limit);

4.5

Trace the following code, assuming that both first and second begin with a value of 1. How many values are printed? while (second <= 10) { System.out.println (second); int temp = first + second; first = second; second = temp; }

5 Second First Temp 1 1 2 2 2 3 3 3 5 5 5 8 8 8 13

translates high-level language program into machine language program.

A compiler

What is the syntax error in the program below? public class MyError { public static void method(String[] args) { System.out.print("Hello, Goodbye"); } }

The name of the method must be main

Which of the following can be used to display float variable fraction with two decimal digits?

System.out.printf("%.2f", fraction);

Insert the missing condition in the following code fragment. The code is intended to read a person's year of birth, which must be a value larger than 1900 but smaller than 2010.The loop continues to prompt the user until a valid value is entered. int birthYear = in.nextInt(); while ( ____________ ) { System.out.print("Invalid value for year. Enter another value: "); birthYear = in.nextInt();

(birthYear <= 1900) || (birthYear >= 2010)

Which of the following lines is not a Java comment? A. /** comments */ B. // comments C. -- comments D. /* comments */ E. ** comments **

-- comments ** comments **

The extension name of a Java bytecode file is

.class

The extension name of a Java source code file is

.java

What output does this for loop generate? for (int j = 0; j < 5; j++) { int value = j * 2; System.out.print(value + ", ");

0, 2, 4, 6, 8,

Java compiler translates Java source code into ____

A. Java bytecode

the following are the reserved words:

A. public B. static C. void D. class

contains predefined classes and interfaces for developing Java programs.

API

Select a condition to complete the code fragment below, which is designed to simulate one hundred coin tosses. for (int j = 1; j <= 100; j++) { if ( ______________ ) { System.out.println("Heads"); } else { System.out.println("Tails"); } }

Math.random() > 0.5

Which of the following statements is correct?

Every statement in a program must end with a semicolon.

Which of the following is the correct way to create a Scanner object to read text from an input file data.txt?

File theFile = new File("data.txt"); Scanner inputFile = new Scanner(theFile);

The __________ method displays a message dialog box

JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Welcome to Java!");

________ is interpreted.

Java

___ provides an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface.

Java IDE

_____ consists of a set of separate programs for developing and testing Java programs, each of which is invoked from a command line.

Java JDK

_____ is a software that interprets Java bytecode

Java virtual machine

What output is generated by the statements below? System.out.println("New"); System.out.print("York");

New York

Which of the following is the correct way to create a PrintWriter object to write text to an output file results.txt?

PrintWriter outputFile = new PrintWriter("results.txt");

Show the output generated by the code segment below. String destination = "Empire-State-Building"; System.out.print(destination.substring(7, 12));

State

Which of the following statements is correct to display Welcome to Java on the console?

System.out.println("Welcome to Java"); System.out.print("Welcome to Java");

Suppose you define a Java class as follows: public class Test { } In order to compile this program, the source code should be stored in a file named

Test.java

What is the problem with the following algorithm? Repeat a number of times Add sales amount to total sales.

The algorithm is ambiguous because it does not specify how many times to repeat the Add statement

Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even!"); }

The program runs and displays It is even!. Explanation: It is a common mistake to use the = operator in the condition test. What happens is that true is assigned to even when you write even = true. So even is true. The program compiles and runs fine and prints 'It is even!'.

If you assume that weight has value 5.87863, what output is generated by the statement below? System.out.printf("The weight is %4.2f", weight);

The weight is 5.88

What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0?

There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true.

In Java, the word true is ___

a Boolean literal

If you forget to put a closing quotation mark on a string, what kind error will be raised?

a compilation error

If a program compiles fine, but it produces incorrect result, then the program suffers ___

a logic error

Every statement in Java ends with ___

a semicolon (;)

Java ___________ can run from a Web browser

applets

A block is enclosed inside __________.

braces

A Java compiler translates source code into

class files

A programmer wishes to insert a comment following an assignment statement. Which of the following will generate an error?

cost = 25.0; // initial cost of a product cost = 25.0; /* initial cost of a product */ cost = 25.0; /* initial cost of a product // x cost = 25.0; // initial cost of a product */

Assuming that variables dollars and cost have been declared, which of the following assignment statements is INVALID?

dollars = cost; cost = dollars + 100; cost = cost + 50; dollars + 100 = cost; x

Suppose variable average is to contain the average (including possible decimal values) of four integer variables: a, b, c, and d. Which of the following statements would perform the correct operation?

double average = (a + b + c + d) / 4.0;

Programming style is important, because __________

good programming style makes a program more readable good programming style helps reduce programming errors

Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.

if (isPrime)

Which of the following code displays the area of a circle if the radius is positive.

if (radius > 0) System.out.println(radius * radius * 3.14159);

To use JOptionPane in your program, you may import it using:

import javax.swing.JOptionPane; import javax.swing.*;

Which of the following represents a correct way to declare and initialize a variable named counter?

int counter = 1;

Which JDK command is correct to run a Java application in ByteCode.class?

java ByteCode.class

The JDK command to compile a class in the file Test.java is

javac Test.java

Insert the missing condition in the following code fragment. The code is intended to display the characters in string str in reverse order. for (int k = str.length() - 1; _______; k--) { System.out.print(str.charAt(k)); }

k >= 0

Select a pseudocode statement to complete the following algorithm, which computes the cost of a shipment. Shipping costs depend on the weight of the item being shipped. The cost is $10 if the item weighs up to five pounds. For heavier items, the cost is $10 plus $2 for each pound in excess of five. cost = $10 If weight is greater than five pounds ________________________ extra charge = $2 x number of excess pounds Add extra charge to cost.

number of excess pounds = weight - 5

!(numberOfDependents > 3 && income <= 50000)

numberOfDependents <= 3 || income > 50000

The main method header is written as:

public static void main(String[] args)

Insert the missing statement in the following code fragment. The method is designed to compute the area of a circle with the radius given as argument. public static double circleArea(double radius) { double area = Math.PI * radius * radius; _______________________ }

return area;

In the statement below, the value stored in variable temp is called _________________. double temp = Math.pow(3, 4);

return value

Insert the missing condition in the following code fragment. The code is intended to increment variable count if the String variable vehicle contains the value "sedan". if (__________) { count++; }

vehicle.equals("sedan")

Insert the missing condition in the following code fragment. The code is intended to add 5 to variable shippingCost if the value of variable weight is at least 10. if (__________) { shippingCost = shippingCost + 5; }

weight >= 10

Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");

x < 0 and z > 0


Conjuntos de estudio relacionados

chapter 3 Stress & Health Psychology

View Set

Chapter 13- The Road To Civil War

View Set

Chapter 5: Nonverbal Communication

View Set

O'Neill, "A Simplified Account of Kant's Ethics"

View Set

Improving Windows Performance IN DEPTH A+ Edition 8

View Set

Physiological Psychology: Master Quiz questions chapters 8-14

View Set

WebBased DSS- Intro to internet & web applications

View Set