Java Quiz 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Write out the header for every project

/* * Firstname Lastname // write all if group * CS A170 * Month dd, yyyy // Month in letters * * Exercise/Exam# */

Complete the program below, named TreeBush.java, to print out the child-like picture of a tree and bush below. The output of this program should match the following, exactly: o oooo ooooooo oooooooo ooIIo o ooo o II oooooo ___II_________ oooo____

/** A program to draw a picture with a tree and bush. */ public class TreeBush { public static void main(String[] args) { /* Your work here */ System.out.println("o"); System.out.println("oooo"); System.out.println("ooooooo"); System.out.println("oooooooo"); System.out.println("ooIIo o ooo"); System.out.println("o II oooooo"); System.out.println(" _____II_________ oooo____"); } }

Complete the program below to print out the last three lines of a poem by Robert Frost. Two roads diverged in a wood, and I -- I took the one less traveled by, And that has made all the difference. by Robert Frost

/** A program to print three lines of a Robert Frost poem followed by an attribution to the poet. */ public class Frost { public static void main(String[] args) { // Your work here System.out.println("Two roads diverged in a wood, and I --"); System.out.println("I took the one less traveled by,"); System.out.println("And that has made all the difference."); System.out.println(" by Robert Frost"); } }

Complete the program below to print out a filled triangle four lines high. In other words, the output of this program should match the following, exactly: x xxx xxxxx xxxxxxx

/** A program to draw a filled triangle. */ public class Triangle { public static void main(String[] args) { System.out.println(" x"); System.out.println(" xxx"); System.out.println(" xxxxx"); System.out.println("xxxxxxx"); /* Your work here */ } }

What will be the output for this? System.out.print("00"); System.out.println(3 + 4);

007

Which of the following statements represents a logic error? 1)System.out.println("The sum of 5 and 6 is 10"); 2)System.out.println(The sum of 5 and 6 is 10); 3)System.out.println(The sum of 5 and 6 is 10"); 4)System.out.println("The sum of 5 and 6 is 11");

1)System.out.println("The sum of 5 and 6 is 10");

Which of the following statements would generate a compile-time error? 1)System.out.println("Java"); 2)System.out.println(Java"); 3)System.out.println(4 + 7); 4)System.out.println("4 + 7");

2)System.out.println(Java");

What is the output? System.out.println("39+3");

39+3

What is the output? System.out.println(39 + 3);

42

What will be the output? public class PrintDemo { public static void main(String[] args) { System.out.println(3 + 4); // Prints 7 System.out.println("Hello"); // Prints "Hello World!" in two lines System.out.println("World!"); System.out.print("00"); // Prints 00 but doesn't start a new line System.out.println(3 + 4); // Prints 7 and starts a new line System.out.println("Goodbye"); } }

7 Hello World! 007 Goodbye

What type of error will this output? public class Error2 { public static void main(String[] args) { System.out.println("Hello, Word!"); // The program doesn't do what it should. } }

A run-time error.

What type of error will this output? public class Error3 { public static void main(String[] args) { System.out.println(1 / 0); // The program terminates. } }

A run-time exception.

Method

A sequence of statements that has a name, may have parameter variables, and may return a value. A method can be invoked any number of times, with different values for its parameter variables.

Statement

A syntactical unit in a program. In Java a statement is either a simple statement, a compound statement, or a block.

Argument

A value supplied in a method call, or one of the values combined by an operator.

Escape characters

Each escape sequence respresents a sinlge character even though it is written with two symbols.

What output is generated by the statements below? System.out.print("Easy as"); System.out.print(" 1 + 2 + 3");

Easy as 1 + 2 + 3

Debugging

Eliminating errors

A Java source file that contains the public class Example1 must be named?

Example1.java

What will this print out? System.out.println("abc\\def");

abc/def

An __________ for solving a problem is a sequence of steps that is unambiguous, executable, and terminating.

algorithm

What is the technical term for values?

arguments

Fred hates his bank. His checking account earns only 0.1% per month, and he is charged a monthly "maintenance fee". Plus, his bank always orders these transactions to maximize their profit. Fred is leaving the country for an extended vacation, and he wonders how much money will be left in his account when he returns. Rearrange the following lines to produce an algorithm that answers his question.

balance = 1000 fee = 5 vacationMonths = 3 interestRate = 0.1 Repeat vacationMonths times: balance = balance - fee interest = balance * interestRate / 100 balance = balance + interest

Building blocks of java example The name of the public class must match the name of the file containing the ______

class

Each __________ contains declarations of methods. Each method contains a sequence of instructions.

class

A ___________ is a violation of the programming language rules that is detected by the compiler.

compile-time error

what does this escape character do? \"

double quote

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

Every program contains at least one class. Choose a class name that describes the program action. What would be an example of this?

public class HelloPrinter {

How to start a program?

public class name { public static void main(String[] args) { system.out.println("??"); } }

Declaring a main method

public static void main(String []args) { ............ }

Every Java program contains a main method with this header. What is an example of this?

public static void main(String[] args) {

A _____________ causes a program to take an action that the programmer did not intend.

run-time error

Each statements end in a?

semicolon ;

The main method contains one or more instructions called_________?

statements

A sequence of characters enclosed in quotation marks is called a?

string

What is the output? System.out.println("there");

there

Use statements such as the following to describe how a value is set or changed:

total cost = purchase price + operating cost Multiply the balance value by 1.05. Remove the first and last character from the word.

What will the error message be for this? System.out.println(1 / 0);

you will get a run-time error message "Division by zero".

In order for a computer program to provide an answer to a problem that computes an answer, it must follow a sequence of steps that is?

•Unambiguous •Executable •Terminating

What type of error will this cause? public class Error1 { public static void main(String[] args) { System.ou.println("Hello, World!"); // } }

-A compile-time error. -Error: /tmp/codecheck.DPYLwbIds9/Error1.java:5: error: cannot find symbol System.ou.println("Hello, World!"); // A compile-time error. ^ symbol: variable ou location: class System 1 error

A method call requires:

-The method you want to use (ex: System.out.println)\ -Any value inside needs to be carried out its tasked in enclosed parentheses (in this case, "Hello,World!")

Correct the errors or type ok if none 1) System.out.print("Hi"); 2) System.out.print("Hi); 3) System.out.print("Hi") 4) System.out.pint("Hi"); 5) system.out.print("Hi"); 6) System.out.print(30 / 2); 7) System.out.print("30 / 0");

1)-Ok;There is nothing wrong with this statement. 2)-System.out.print("Hi"); Each string has opening and closing quotation marks. 3)System.out.print("Hi"); Each statement is followed by a semicolon. 4)System.out.print("Hi"); If you misspell the name of a method, the compiler will not know what you mean. 5)System.out.print("Hi"); The names system and System are different. Java is "case-sensitive". 6)OK;There is nothing wrong with this statement. It prints 15. 7)OK; There is nothing wrong with this statement. It prints 30 / 0. Without the quotation marks, there would have been a run-time error because you cannot divide by zero.

Exception

A class that signals a condition that prevents the program from continuing normally. When such a condition occurs, an object of the exception class is thrown.

Pseudocode

A high-level description of the actions of a program or algorithm, using a mixture of English and informal programming language syntax.

Class

A programmer-defined data type.

String

A sequence of characters

Bug

An error in a program

Run-time error

An error in a syntactically correct program that causes it to act differently from its specification.

Logic error

An error in a syntactically correct program that causes it to act differently from its specification. (A form of run-time error.)

compile-time error (syntax error)

An error that is detected when a program is compiled.

Syntax error

An instruction that does not follow the programming language rules and is rejected by the compiler. (A form of compile-time error.)

Algorithm

An unambiguous, executable, and terminating specification of a way to solve a problem.

What does this escape character do? \\

Backslash

Here, the indentation indicates that both statements should be executed for each car. •Indicate results with statements such as:

Choose car1. Report year as the answer.

_____are the fundamental building blocks of Java programs.

Classes

Use indentation to indicate which statements should be selected or repeated:

For each car operating cost = 10 x annual fuel cost total cost = purchase price + operating cost

What do the following statements print? System.out.println("Hello"); System.out.println(""); System.out.println("World");

Hello World

Building blocks of java example Declaration of a class called _________ public class HelloPrinter

HelloPrinter

Building blocks of java example Class HelloPrinter must be contained in a file named _________?

HelloPrinter.Java

What is the output? System.out.println("Hi");

Hi

What is the output? System.out.print("Hi"); System.out.println("there");

Hithere

Describe decisions and repetitions as follows:

If total cost 1 < total cost 2 While the balance is less than $20,000 For each picture in the sequence

When you used your computer, you may have experienced a program that "crashed" (quit spontaneously) or "hung" (failed to respond to your input). Is that behavior a compile-time error or a run-time error?

It is a run-time error. After all, the program had been compiled in order for you to run it.

System.out.print("Java"); System.out.println("is"); System.out.print("fun");

Java is fun

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

New York

What does this escape character do? \n

New line. Go to the beginning of the next line.

___________ is an informal description of a sequence of steps for solving a problem.

Pseudocode

"Wiki markup" is a simple convention indicating which parts of text should be emphasized, computer code, and so on. You surround the text to be formatted with symbols, such as It is *important* to include a `main` method in every Java program. Here, "important" is emphasized and "main" is computer code. In HTML, the language for web files, a different convention is used. You surround formatted text with tags, like this: It is <em>important</em> to include a <code>main</code> method in every Java program. Note that the tags come in pairs <tagName> and </tagName>. We want to translate Wiki markup into HTML. Rearrange the following lines of code to develop an algorithm for this purpose. The algorithm reads text, a character at a time. If the character is a formatting character (such as *), we must figure out whether to produce a start tag (such as <em>) or and end tag (such as </em>). It is an error if the formatting characters don't match, such as a * character followed by a ` character.

Set currentFormat to "None" While there are more characters to read: c=next character if c is a formatting character'*etc: if currentFormat is "None": Write<,the HTML code for c, and> Set currentFormat to c Else if c is the same as currentFormat: Write </, the HTML code for c, and > Set currentFormat to "None" Else Report an error Else: Write C

A __________ is a sequence of characters enclosed in quotation marks.

String

Which of the following code segments can be used to print the message Hello Goodbye on one output line?

System.out.print("Hello "); System.out.print("Goodbye");

How would you modify the HelloPrinterprogram to print the word hello vertically?

System.out.println("H"); System.out.println("e"); System.out.println("l"); System.out.println("l"); System.out.println("o"); or System.out.println("H\ne\nl\no\no");

The statements inside the main method are executed when the program runs. what is an example of this?

System.out.println("Hello,World"); }

How would you print this sentence?

System.out.println("\"Java\"refers to a language");

What does this escape character do? \t

Tab. add white space up to the next tab stop.

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.

Main Method

The method that is first called when a Java application executes.

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

The name of the method must be main not run

What output is generated by the statements below? System.out.print("The total is "); System.out.print(10+25);

The total is 35

Suppose you omit the "" characters around Hello, World! from the HelloPrinter.java program. Is this a compile-time error or a run-time error?

This is a compile-time error. The compiler will complain that it does not know the meanings of the words Hello and World.

Suppose you change println to printline in the HelloPrinter.java program. Is this a compile-time error or a run-time error?

This is a compile-time error. The compiler will complain that system.outdoes not have a method called printline.

Why can't you test a program for run-time errors when it has compiler errors?

When a program has compiler errors no class file is produced and there is nothing to run.

Every Java application contains a class with a ____________. When the application starts, the instructions in the _____________ are executed.

main method

A __________ is called by specifying the ________ and its arguments.

method

Every java application must have a main_______?

method

What will this print out? System.out.println("new\nline");

new line

In Java, every program must have?

one class

Print this in java Hello, World! Hello, All!

public class HelloAll { public static void main (String[] args) { System.out.println("Hello, World!"); System.out.println("Hello, All!"); } }


संबंधित स्टडी सेट्स

Chapter 11 (The Americas 2500 b.c.e.-1500 c.e.)

View Set

Chapter 1.3 Describing Quantitative Data with Numbers

View Set

MGMT 383: Study guide chapter 13

View Set

Chapter 12: Transcription and Translation

View Set

Chapter 10. Campaigns and Elections

View Set

Intro to business Chapter 3 (In Progress)

View Set

Accounting Chapter 1 Multiple Choice

View Set