Chapter 2 Java
what is the output produced by the following lines of program code? double result = (1/2)*2; System.out.println("(1/2)*2 equals "+result);
(1/2)* 2 is equal to 0.0
char symbol = '7'; System.out.println((int)symbol); Can you write an expression that will work to produce the integer that intuitively corresponds to the digit in symbol, assuming that symbol contains one of the ten digits 0 through 9?
(int)symbol - (int) '0'
What output is produced by the following statements? String test= "abcdefg"; System.out.println(test.length()); System.out.println(test.charAt(1));
7 b
What is the value of the expression s1.equals(s2) after the following statements execute? String s1 = "Hello John"; String s2= "hello john";
False, because the strings are not equal.
What output is produced by the following statements? String test = "Hello John"; test = test.toUpperCase(); System.out.println(test);
HELLO JOHN
What is the output produced by the following Java code: /** Code for Question 33 . */ System.out.println("One"); //System.out.pintln("Two"); System.out.println("And hit it!");
One, And hit it
What output is produced by the following statements? String s= "Hello"+""+"Joe"; System.out.println(s);
Since the empty string is the second of the three strings, the output is HelloJoe
Write Java statements that will cause the following to be written to the screen: Once upon a time, there were three little programmers.
System.out.println("Once upon a time,"); System.out.println("there were three little programmers.");
what are the kinds of comments in Java?
There are //comments,/**/ comments, and /***/ comments. Everything following a // on the same line is a comment. Everything between a /* and a matching*/ is a comment.Everything between a /** and a matching */ is a comment that is recognized by the program java doc.
Although it is kind of silly, legislators have been known to pass laws that "change" the value of pi. Suppose you live in a state where, by law, the value of pi is exactly 3.14. How must you change the program in Listing 2.8 to make it comply with the law?
change public static final double PI = 3.14159; to public static final double PI=3.14;
Write a java assignment statement that will increase the value of the variable count by 3. The variable is of type int.
count = count+3;
Give the declaration for a variable called count of type int. The variable should be initialized to zero in the declaration.
int count = 0;
What output is produced by the following lines of program code? int n = 2; n++; System.out.println(" n is " + n); n--; System.out.println("n is "+n);
n is 3 n is 2
Give a definition for a named constant for the number of hours in a day.
public static final int HOURS_PER_DAY =24;
What is the output produced by the following lines of program code? char a, b; a = 'b'; System.out.println(a); b = 'c'; System.out.println(b); a = b; System.out.println(a)b;
b c c
What is the difference between the methods System.out.println and System.out.print?
The method System.out.println displays its output and the advances to the next line. Subsequent output would begin on this next line. In contrast, System.out.println displays its output but does not advance to the next line. Thus,subsequent output would begin on the same line.
Consider the following statement from the program in Listing 2.3: System.out.println(originalAmount+" cents in coins can be given as:"); Suppose that you replaced the preceding line with the following: System.out.println(amount+"cents in coins can be given as:"); How would this change the sample output given in Listing 2.3?
The output would change to the following: Enter a whole number from 1 to 99 I will find a combination of coins that equals that amount of change. 87 2 cents in coins can be given as: 3 quarters, a dime, and 2 pennies.
What is the value of the expression s1.equals(s2) after the following statements execute? String s1 = "Hello John"; String s2= "hello john"; s1=s1.toUpperCase(); s2=s2.toUpperCase();
True, because the strings are equal.
What output is produced by the following statements? System.out.println("abc\ndef");
abc def
What output is produced by the following statements? System.out.println("abc\\ndef");
abc/ndef
What output is produced by the following statements? String test="abcdefg"; System.out.println(test.substring(3));
defg
Give the declaration for two variables of type double. The variables are to be named rate and time. Both variables should be initialized to zero in the declaration.
double rate = 0.0, time = 0.0;
Write a complete Java program that reads a line of keyboard input containing two values of type int-separated by one or more spaces= and then displays the two numbers.
import java.util.Scanner; public class Questoin29 { public static void main(String[]args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter two whole numbers:"); int n1 = keyboard.nextInt(); int n2=keyboard.nextInt(); System.out.println("You entered: "+n1+" "+ n2); } }
Write a Java assignment statement that will set the value of the variable interest to the value of the variable balance multiplied by 0.05.
interest = 0.05*balance; interest= balance*0.05;
Write a java assignment statement that will set the value of the variable interest to the value of the variable balance multiplied by the value of the variable rate. The variables are of type double.
interest = balance*rate;
What is the output produced by the following lines of program code?
quotient =2 and remainder = 1
Which of the following may be used as variable names in java? rate1, 1stPlayer, myprogram.java, long, TimeLimit, numberofWindow
rate1, numberofWindows
What is the output produced by the following code? int result = 3*7%3-4-6; System.out.println("result is " + result);
result is -10
What output is produced by the following statements? String greeting = "How do you do": System.out.println(greeting + "Seven of Nine.");
How do you doSeven of Nine.
What is the normal spelling convention for named constants?
The normal practice of programmers is to spell named constants with all uppercase letters, using the underscore symbol to separate words.
Can a Java program have two different variables with the names aVariable and avariable?
Yes, because the v's uses different capitalization.
Write a complete Java program that reads one line of text containing exactly three words- separated by any kind or amount of whitespace- and then displays the line with spacing corrected as follows: The output has no space before the first word and exactly two spaces between each pair of adjacent words.
import java.util.Scanner; public class Questoin30 { public static void main(String[]args) { Scanner keyboard = new Scanner(System.in); String word, word2,word3; System.out.println("Type three words on one line: "); word1=keyboard.next(); word2=keyboard.next(); word3=keyboard.next(); System.out.println("You tyoed the words"); System.out.println(word1+" "+word2+" "+word3); } }
Write the declaration for two variables called miles and flowRate. Declare the variable miles to be of type int and initialize it to zero in the declaration. Declare the variable flowRate to be of type double and initialize it to 50.56 in the declaration.
int miles=0; double flowRate = 50.56;
What is the output produced by the following code? int result = 11; result/=2; System.out.println("result is the "+ result);
result is 5