CH1 SELF CHECK

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

f) Replace lines 3-4 with: System.out.println("The first rule of Java Club is,");

Different program output: The output would now have no line break between "The first rule" and "of Java Club is," in its output.

Rewrite the following code as a series of equivalent System.out.println statements (i.e., without any System.out.print statements): System.out.print("Twas "); System.out.print("brillig and the "); System.out.println(" "); System.out.print(" slithy toves did"); System.out.print(" "); System.out.println("gyre and"); System.out.println( "gimble"); System.out.println(); System.out.println( "in the wabe." );

Equivalent code without System.out.print statements: System.out.println("Twas brillig and the "); System.out.println(" slithy toves did gyre and"); System.out.println("gimble"); System.out.println(); System.out.println("in the wabe.");

Why do computers use binary numbers? - Because there are 10 types of people: Those who understand binary, and those who don't. - It makes the computer faster and more efficient than other number bases. - It is easier to build electronic devices reliably if they only have to distinguish between two electric states. - At the time when computers were invented, humans only knew about the numbers 0 and 1.

It is easier to build electronic devices reliably if they only have to distinguish between two electric states.

What is the output of the following program? Note that the program contains several comments. public class Commentary { public static void main(String[] args) { System.out.println("some lines of code"); System.out.println("have // characters on them"); System.out.println("which means "); // that they are comments // System.out.println("written by the programmer."); System.out.println("lines can also"); System.out.println("have / and "); System.out.println(/ characters"); /* "which represents" System.out.println("a multi-line style"); */ System.out.println("of comment."); } }

Output of Commentary program: some lines of code have // characters on them which means lines can also have / and / characters of comment.

What is the output produced from the following statements? - System.out.println("\"Quotes\""); - System.out.println("Slashes \\//"); - System.out.println("How '\"confounding' \"\\\" it is!");

Output of statements: "Quotes" Slashes \// How '"confounding' "\" it is!

Which of the following can be used in a Java program as identifiers? Check all of the identifiers that are legal. sum_of_data "hello" _average B4 first-name 42isThesolution for ABC AnnualSalary Println

The legal identifiers shown are println, AnnualSalary, ABC, sum_of_data, _average, B4.

Write a println statement that produces the following output: / \ // \\ /// \\\

println statement to produce desired output: System.out.println("/ \\ // \\\\ /// \\\\\\");

What series of println statements would produce the following output? This is a test of your knowledge of "quotes" used in 'string literals.' You're bound to "get it right" if you read the section on ''quotes.''

println statements to produce desired output: System.out.println("This is a test of your"); System.out.println("knowledge of \"quotes\" used"); System.out.println("in 'string literals.'"); System.out.println(); System.out.println("You're bound to \"get it right\""); System.out.println("if you read the section on"); System.out.println("''quotes.''");

What is the decimal equivalent of each of the following binary numbers? a. 100 = b. 1011 = c. 101010 = d. 1001110 =

a. 100 = 4 b. 1011 = 11 c. 101010 = 42 d. 1001110 = 78

Convert each of the following decimal numbers into its equivalent binary number: a. 6 = b. 44 = c. 72 = d. 131 =

a. 6 = 110 b. 44 = 101100 c. 72 = 1001000 d. 131 = 10000011

Which of the following method headers uses the correct syntax? public static example void[] { public void static example() { public void static example{} ( public static void example() { public static example() {

public void static example() {

b) Change line 9 to: public static void MAIN(String[] args) {

Different program output: The program would not run because Java would be unable to find the main method.

c) Insert a new line after line 11 that reads: System.out.println();

Different program output: There would now be a blank line between the two printed messages.

In your own words, describe an algorithm for baking cookies. Assume that you have a large number of hungry friends, so you'll want to produce several batches of cookies!

Make the cookie batter: Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Bake the cookies: Set the oven for the appropriate temperature. Set the timer. Place the cookies into the oven. Allow the cookies to bake. Add frosting and sprinkles: Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies.

The following program contains four errors. Correct the errors and submit a working version of the program. public class FamousSpeech public static void main(String[]) { System.out.println("Four score and seven years ago,"); System.out.println("our fathers brought forth on"); System.out.println("this continent a new nation"); System.out.println("conceived in liberty,"); System.out.println("and dedicated to the proposition"); System.out.println("that"); /* this part should System.out.println("all"); really say, System.out.println("men"); "all PEOPLE!" /* System.out.println("are"; System.out.println("created"); System.out.println("equal"); } }

Mistakes in FamousSpeech program: line 1: A { brace is missing. line 2: The word args is missing. line 8: The comment on lines 8-10 accidentally comments out lines 9-10 of the program. Using // comments would fix the problem. line 11: A closing right parenthesis ) is missing.

The following program contains at least 10 errors. Correct the errors and submit a working version of the program. public class LotsOf Errors { public static main(String args) { System.println(Hello, world!); message() } public static void message { System.out println("This program surely cannot "; System.out.println("have any "errors" in it"); }

Mistakes in LotsOfErrors program: line 1: The class name should be LotsOfErrors (no space). line 2: The word void should appear after static. line 2: String should be String[]. line 3: System.println should be System.out.println. line 3: "Hello, world!) should be "Hello, world!"). line 4: There should be a semicolon after message(). line 7: There should be a () after message. line 8: System.out println should be System.out.println. line 8: cannot"; should be cannot");. line 9: The phrase "errors" cannot appear inside a String. 'errors' or \"errors\" would work. line 11: There should be a closing } brace.

The following program contains three errors. Correct the errors and submit a working version of the program. public MyProgram { public static void main(String[] args) { System.out.println("This is a test of the") System.out.Println("emergency broadcast system."); } }

Mistakes in MyProgram program: line 1: The keyword class is missing. line 3: A semicolon is missing. line 4: The word Println should not be capitalized.

The following program contains three errors. Correct the errors and submit a working version of the program. public class SecretMessage { public static main(string[] args) { System.out.println("Speak friend"); System.out.println("and enter); }

Mistakes in SecretMessage program: line 2: The keyword void is missing. line 2: The word string should be capitalized. line 4: A closing " mark is missing. line 5: A closing } brace is missing.

What is the difference between the file MyProgram.java and the file MyProgram.class? - The programmer writes the .class file first, and the .java file is generated automatically later. - MyProgram.java is a source code file typed by the programmer, and MyProgram.class is a compiled executable class file that is run by the computer. - The .class file is for object-oriented programming and the .java file is for procedural programming. - A .java file is a much larger binary file and a .class file is a smaller compressed version. - A .java file contains code written in the Java language, and a .class file contains code written in the C++language.

MyProgram.java is a source code file typed by the programmer, and MyProgram.class is a compiled executable class file that is run by the computer.

e) Change line 2 to: public static void showMessage() {, and change lines 11 and 12 to: showMessage();

No effect: The program would still compile successfully and produce the same output.

What is the output from the following Java program? public class Confusing { public static void method1() { System.out.println("I am method 1."); } public static void method2() { method1(); System.out.println("I am method 2."); } public static void method3() { method2(); System.out.println("I am method 3."); method1(); } public static void main(String[] args) { method1(); method3(); method2(); method3(); } }

Output of Confusing program: I am method 1. I am method 1. I am method 2. I am method 3. I am method 1. I am method 1. I am method 2. I am method 1. I am method 2. I am method 3. I am method 1.

What is the output produced from the following program? You may wish to draw a structure diagram first. This is a slightly modified version of the previous problem, Confusing. public class Confusing { public static void main(String[] args) { method1(); method3(); method2(); method3(); } public static void method2() { method1(); System.out.println("I am method 2."); } public static void method3() { method1(); method2(); System.out.println("I am method 3."); } public static void method1() { System.out.println("I am method 1."); } }

Output of Confusing2 program: I am method 1. I am method 1. I am method 1. I am method 2. I am method 3. I am method 1. I am method 2. I am method 1. I am method 1. I am method 2. I am method 3.

What is the output produced from the following program? You may wish to draw a structure diagram first. This is a slightly modified version of the previous problem, Confusing2. public class Confusing { public static void main(String[] args) { method2(); method1(); method3(); method2(); } public static void method2() { method1(); System.out.println("I am method 2."); } public static void method3() { method2(); System.out.println("I am method 3."); method1(); } public static void method1() { System.out.println("I am method 1."); } }

Output of Confusing3 program: I am method 1. I am method 2. I am method 1. I am method 1. I am method 2. I am method 3. I am method 1. I am method 1. I am method 2.

What is the output produced from the following program? You may wish to draw a structure diagram first. public class Strange { public static void main(String[] args) { first(); third(); second(); third(); } public static void first() { System.out.println("Inside first method."); } public static void second() { System.out.println("Inside second method."); first(); } public static void third() { System.out.println("Inside third method."); first(); second(); } }

Output of Strange program: Inside first method Inside third method Inside first method Inside second method Inside first method Inside second method Inside first method Inside third method Inside first method Inside second method Inside first method

What is the output produced from the following program? You may wish to draw a structure diagram first. This is a slightly modified version of the previous problem, Strange. public class Strange { public static void main(String[] args) { second(); first(); second(); third(); } public static void first() { System.out.println("Inside first method."); } public static void second() { System.out.println("Inside second method."); first(); } public static void third() { System.out.println("Inside third method."); first(); second(); } }

Output of Strange2 program: Inside first method Inside first method Inside second method Inside first method Inside third method Inside second method Inside first method Inside first method Inside second method Inside first method Inside third method

What is the output produced from the following program? You may wish to draw a structure diagram first public class Tricky { public static void main(String[] args) { message1(); message2(); System.out.println("Done with main."); } public static void message1() { System.out.println("This is message1."); } public static void message2() { System.out.println("This is message2."); message1(); System.out.println("Done with message2."); } }

Output of Tricky program: This is message1. This is message2. This is message1. Done with message2. Done with main.

What is the output produced from the following statements? (Treat tabs as aligning to every multiple of eight spaces.) - System.out.println("\ta\tb\tc"); - System.out.println("\\\\"); - System.out.println("'"); - System.out.println("\"\"\""); - System.out.println("C:\nin\the downward spiral");

Output of statements: a b c \\ ' """ C: in he downward spiral

What is the output from the following Java program? (Assume that a tab is expanded into eight spaces.) public class Letter { public static void main(String[] args) { System.out.println("Dear \"DoubleSlash\" magazine,"); System.out.println(); System.out.println("\tYour publication confuses me. Is it"); System.out.println("a \\\\ slash or a //// slash?"); System.out.println("\nSincerely,"); System.out.println("Susan \"Suzy\" Smith"); } }

Output of statements: Dear "DoubleSlash" magazine, Your publication confuses me. Is it a \\ slash or a //// slash? Sincerely, Susan "Suzy" Smith

What is the output produced from the following statements? - System.out.println("Shaq is 7'1"); - System.out.println("The string \"\" is an empty message."); - System.out.println("\\'\"\"");

Output of statements: Shaq is 7'1 The string "" is an empty message. \'""

What is the output produced from the following statements? (Treat tabs as aligning to every multiple of eight spaces.) - System.out.println("name\tage\theight"); - System.out.println("Archie\t17\t5'9\""); - System.out.println("Betty\t17\t5'6\""); - System.out.println("Jughead\t16\t6'");

Output of statements: name age height Archie 17 5'9" Betty 17 5'6" Jughead 16 6'

The following program is legal under Java's syntax rules, but it is difficult to read because of its layout and lack of comments. Reformat it using the rules given in Chapter 1, and add a comment header at the top of the program. (Practice-It isn't really able to check that you satisfied the requirements of this question. You will receive credit as long as you supply a reasonable amount of indentation and add two lines of // comments.) public class GiveAdvice{ public static void main (String[ ]args){ System.out.println ( "Programs can be easy or"); System.out.println( "difficult to read, depending" ); System.out.println("upon their format.") ;System.out.println();System.out.println( "Everyone, including yourself,"); System.out.println ("will be happier if you choose"); System.out.println("to format your programs." ); } }

Reformatted version of GiveAdvice program: // Suzy Student, Fall 2048 // This program prints a message about proper formatting // of Java programs. public class GiveAdvice { public static void main(String[] args) { System.out.println("Programs can be easy or"); System.out.println("difficult to read, depending"); System.out.println("upon their format."); System.out.println(); System.out.println("Everyone, including yourself,"); System.out.println("will be happier if you choose"); System.out.println("to format your programs."); } }

The following program is legal under Java's syntax rules, but it is difficult to read because of its layout and lack of comments. Reformat it using the rules given in Chapter 1, and add a comment header at the top of the program. (Practice-It isn't really able to check that you satisfied the requirements of this question. You will receive credit as long as you supply a reasonable amount of indentation and add two lines of // comments.) public class Messy{public static void main(String[]args){message () ;System.out.println() ; message ( );} public static void message() { System.out.println( "I really wish that" );System.out.println ("I had formatted my source") ;System.out.println("code correctly!");}}

Reformatted version of Messy program: // Suzy Student, Fall 2048 // This program prints a cautionary message about messy formatting // of Java programs. public class Messy { public static void main(String[] args) { message(); System.out.println(); message(); } public static void message() { System.out.println("I really wish that"); System.out.println("I had formatted my source"); System.out.println("code correctly!"); } }

a) Change line 1 to: public class Demonstration

Syntax error: The program would not compile because its class name (Demonstration) would not match its file name (Example.java).

d) Change line 2 to: public static void printMessage() {

Syntax error: The program would not compile because the main method would be calling a method (displayRule) that no longer existed.

What series of println statements would produce the following output? "Several slashes are sometimes seen," said Sally. "I've said so." See? \ / \\ // \\\ ///

System.out.println("\"Several slashes are sometimes seen,\""); System.out.println("said Sally. \"I've said so.\" See?"); System.out.println("\\ / \\\\ // \\\\\\ ///");

Which of the following is the correct syntax to output a message? - System.println.out('Hello, world!'); - Out.system.println"(Hello, world!)"; - System.out.println("Hello, world!"); - System.println(Hello, world!); - System.println("Hello, world!");

System.println(Hello, world!);

Consider the following program, saved into a file named Example.java: 1 public class Example { 2 public static void displayRule() { 3 System.out.println("The first rule "); 4 System.out.println("of Java Club is,"); 5 System.out.println(); 6 System.out.println("you do not talk about Java Club."); 7 } 8 9 public static void main(String[] args) { 10 System.out.println("The rules of Java Club."); 11 displayRule(); 12 displayRule(); 13 } 14 }

What would happen if each of the following changes were made to the Example program? If there would be no change to the program, choose "nothing". If it would cause the program not to compile, choose "error". If it would change the program's output, choose "output". Treat each change independently of the others.


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

Financial Management Midterm Chapters 1-5

View Set

NCLEX practice questions documentation

View Set

Henry David Thoreau - "Civil Disobedience"

View Set