Home  | 

 

 ONLINE WEB SESSION  A 

Covering Gaddis chapters 1 and 2 and 3


Answer the following multiple choice questions for chapters 1 and 2 and 3

Multiple Choice ch 1

  1. These are words that have a special meaning in the programming language.
    1. punctuation
    2. programmer-defined names
    3. key words
    4. operators
  1. These are symbols or words that perform operations on one or more operands.
    1. punctuation
    2. programmer-defined names
    3. key words
    4. operators
  1. These characters serve specific purposes, such as marking the beginning or ending of a statement, or separating items in a list.
    1. punctuation
    2. programmer-defined names
    3. key words
    4. operators
  1. These are words or names that are used to identify storage locations in memory and parts of the program that are created by the programmer.
    1. punctuation
    2. programmer-defined names
    3. key words
    4. operators
  1. These are the rules that must be followed when writing a program.
    1. syntax
    2. punctuation
    3. key words
    4. operators
  1. This is a named storage location in the computer’s memory.
    1. class
    2. key word
    3. variable
    4. operator
  1. The Java compiler generates      .
    1. machine code
    2. byte code
    3. source code
    4. HTML
  1. JVM stands for      .
    1. Java Variable Machine
    2. Java Variable Method
    3. Java Virtual Method
    4. Java Virtual Machine

Multiple Choice  ch 2

  1. Every complete statement ends with a      .
    1. period
    2. parenthesis
    3. semicolon
    4. ending brace
  1. The following data

   72

   'A'

   "Hello World"

   2.8712

are all examples of      .

    1. variables
    2. literals
    3. strings
    4. none of these
  1. A group of statements, such as the contents of a class or a method, are enclosed in      .
    1. braces {}
    2. parentheses ()
    3. brackets []
    4. any of these will do
  1. Which of the following are not valid assignment statements? (Indicate all that apply.)
    1. total = 9;
    2. 72 = amount;
    3. profit = 129
    4. letter = 'W';
  1. Which of the following are not valid println statements? (Indicate all that apply.)
    1. System.out.println + "Hello World";
    2. System.out.println("Have a nice day");
    3. out.System.println(value);
    4. println.out(Programming is great fun);
  1. The negation operator is      .
    1. unary
    2. binary
    3. ternary
    4. none of these
  1. This key word is used to declare a named constant.
    1. constant
    2. namedConstant
    3. final
    4. concrete

 

  1. These characters mark the beginning of a multi-line comment.
    1. //
    2. /*
    3. */
    4. /**
  1. These characters mark the beginning of a single-line comment.
    1. //
    2. /*
    3. */
    4. /**
  1. These characters mark the beginning of a documentation comment.
    1. //
    2. /*
    3. */
    4. /**
  1. Which Scanner class method would you use to read a string as input?
    1. nextString
    2. nextLine
    3. readString
    4. getLine
  1. Which Scanner class method would you use to read a double as input?
    1. nextDouble
    2. getDouble
    3. readDouble
    4. None of these; you cannot read a double with the Scanner class
  1. You can use this class to display dialog boxes.
    1. JOptionPane
    2. BufferedReader
    3. InputStreamReader
    4. DialogBox
  1. When Java converts a lower-ranked value to a higher-ranked type, it is called a(n)      .
    1. 4-bit conversion
    2. escalating conversion
    3. widening conversion
    4. narrowing conversion
  1. This type of operator lets you manually convert a value, even if it means that a narrowing conversion will take place.
    1. cast
    2. binary
    3. uploading
    4. dot

 

Multiple Choice  ch 3

 

  1. The if statement is an example of a                   .
    1. sequence structure
    2. decision structure
    3. pathway structure
    4. class structure

 

  1. This type of expression has a value of either true or false.
    1. binary expression
    2. decision expression
    3. unconditional expression
    4. boolean expression

 

  1. >, <, and == are                   .
    1. relational operators
    2. logical operators
    3. conditional operators
    4. ternary operators

 

  1. &&, ||, and ! are                   .
    1. relational operators
    2. logical operators
    3. conditional operators
    4. ternary operators
  1. This is an empty statement that does nothing.
    1. missing statement
    2. virtual statement
    3. null statement
    4. conditional statement
  1. To create a block of statements, you enclose the statements in these.
    1. parentheses()
    2. square brackets []
    3. angled brackets <>
    4. braces {}
  1. This is a boolean variable that signals when some condition exists in the program.
    1. flag
    2. signal
    3. sentinel
    4. siren
  1. How does the character ‘A’ compare to the character ‘B’?
    1. ‘A’ is greater than ‘B’
    2. ‘A’ is less than ‘B’
    3. ‘A’ is equal to ‘B’
    4. You cannot compare characters

 

  1. This is an if statement that appears inside another if statement.
    1. nested if statement
    2. tiered if statement
    3. dislodged if statement
    4. structured if statement
  1. An else clause always goes with                   .
    1. the closest previous if clause that doesn’t already have its own else clause
    2. the closest if clause
    3. the if clause that is randomly selected by the compiler
    4. none of these
  1. When determining whether a number is inside a range, it’s best to use this operator.
    1. &&
    2. !!
    3. ||
    4. ? :

 

  1. This determines whether two different String objects contain the same string.
    1. the == operator
    2. the = operator
    3. the equals method
    4. the stringCompare method
  1. The conditional operator takes this many operands.
    1. one
    2. two
    3. three
    4. four
  1. This section of a switch statement is branched to if none of the case expressions match the switch expression.
    1. else
    2. default
    3. case
    4. otherwise
  1. You can use this method to display formatted output in a console window.
    1. Format.out.println
    2. Console.format
    3. System.out.printf

             d.   System.out.formatted


 

DEFINE THE FOLLOWING TERMS

data types

String

literal

attributes

Scanner

JOptionPane

system exit

byte code

List the 4 types of program structures

packages

 import

if

if, else

case

switch

break

modulus

class and method

Fina

 

 

2 REPRESENTATIVE SAMPLE PROGRAMS FOR CHAPTER 2

SIMILAR PROGRAMS  are in the chapter examples that you downloaded

optional - not to be turned in

type in your name and RUN THEM BOTH using JGrasp

THE FIRST USES Scanner

import java.util.Scanner;

/**
This program demonstrates using Scanner

Type your whole name here
*/

public class PayrollScanner
{
public static void main(String[] args)
{
String name;
int hours;
double payRate;
double grossPay;

Scanner keyboard = new Scanner(System.in); // Setup Scanner to read input

System.out.print("What is your name? ");
name = keyboard.nextLine();

System.out.print("How many hours did you work this week? ");
hours = keyboard.nextInt();

System.out.print("What is your hourly pay rate? ");
payRate = keyboard.nextDouble();

grossPay = hours * payRate;


System.out.println("Hello, " + name + ", Your gross pay is $" + grossPay);
}
}

 

THE SECOND USES JOptionPane

import javax.swing.JOptionPane;


/**
This program demonstrates using JOptionPane

Type your whole name here
*/


public class PayrollJOption
{
public static void main(String[] args)
{
String name;
int hours;
double payRate;
double grossPay;

String inputString; // For reading input with JOptionPane



name = JOptionPane.showInputDialog("Enter " + "your name? ");


inputString = JOptionPane.showInputDialog("How many hours " +
"did you work this week? ");
hours = Integer.parseInt(inputString);


inputString = JOptionPane.showInputDialog("What is your " +
"hourly pay rate? ");
payRate = Double.parseDouble(inputString);


grossPay = hours * payRate;



JOptionPane.showMessageDialog(null, "Hello " + name +
", Your gross pay is $" + grossPay);

System.exit(0);
}
}

2 REPRESENTATIVE SAMPLE PROGRAMS FOR CHAPTER 3

SIMILAR PROGRAMS  are in the chapter examples that you downloaded

optional - not to be turned in

type in your name and RUN THEM BOTH  using JGrasp

THE FIRST USES IF STATEMENTS

import javax.swing.JOptionPane;


public class LoanQualifier
{
public static void main(String[] args)
{
double salary;
double yearsOnJob;
String input;


input = JOptionPane.showInputDialog("Enter your " +
"annual salary.");
salary = Double.parseDouble(input);


input = JOptionPane.showInputDialog("Enter the number of " +
"years at your current job.");
yearsOnJob = Double.parseDouble(input);


if (salary >= 30000)
{
if (yearsOnJob >= 2)
{
JOptionPane.showMessageDialog(null, "You qualify " +
"for the loan.");
}
else
{
JOptionPane.showMessageDialog(null, "You must have " +
"been on your current job for at least " +
"two years to qualify.");
}
}
else
{
JOptionPane.showMessageDialog(null, "You must earn " +
"at least $30,000 per year to qualify.");
}

System.exit(0);
}
}

THE SECOND  USES THE SWITCH STATEMENT

import java.util.Scanner;


public class SwitchDemo
{
public static void main(String[] args)
{
int number;

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter 1, 2, or 3: ");
number = keyboard.nextInt();

switch (number)
{
case 1:
System.out.println("You entered 1.");
break;
case 2:
System.out.println("You entered 2.");
break;
case 3:
System.out.println("You entered 3.");
break;
default:
System.out.println("That's not 1, 2, or 3!");
}
}
}

 

 

 

 

 

HINTS FOR CHAPTER 2

VERY CAREFULLY Read the following hints for correcting your JAVA programs

Mismatched braces, quotation marks, or parentheses. In this chapter you saw that the statements making up a class definition are enclosed in a set of braces. Also, you saw that the statements in a method are also enclosed in a set of braces. For every opening brace, there must be a closing brace in the proper location. The same is true of double-quotation marks that enclose string literals and single-quotation marks that enclose character literals. Also, in a statement that uses parentheses, such as a mathematical expression, you must have a closing parenthesis for every opening parenthesis.

·         Misspelling key words. Java will not recognize a key word that has been misspelled.

·         Using capital letters in key words. Remember that Java is a case-sensitive language, and all key words are written in lowercase. Using an uppercase letter in a key word is the same as misspelling the key word.

·         Using a key word as a variable name. The key words are reserved for special uses; they cannot be used for any other purpose.

·         Using inconsistent spelling of variable names. Each time you use a variable name, it must be spelled exactly as it appears in its declaration statement.

·         Using inconsistent case of letters in variable names. Because Java is a case-sensitive language, it distinguishes between uppercase and lowercase letters. Java will not recognize a variable name that is not written exactly as it appears in its declaration statement.

·         Inserting a space in a variable name. Spaces are not allowed in variable names. Instead of using a two-word name such as gross pay, use one word, such as grossPay.

·         Forgetting the semicolon at the end of a statement. A semicolon appears at the end of each complete statement in Java.

·         Assigning a double literal to a float variable. Java is a strongly typed language, which means that it only allows you to store values of compatible data types in variables. All floating-point literals are treated as doubles, and a double value is not compatible with a float variable. A floating-point literal must end with the letter f or F in order to be stored in a float variable.

·         Using commas or other currency symbols in numeric literals. Numeric literals cannot contain commas or currency symbols, such as the dollar sign.

·         Unintentionally performing integer division. When both operands of a division statement are integers, the statement will result in an integer. If there is a remainder, it will be discarded.

·         Forgetting to group parts of a mathematical expression. If you use more than one operator in a mathematical expression, the expression will be evaluated according to the order of operations. If you wish to change the order in which the operators are used, you must use parentheses to group part of the expression.

·         Inserting a space in a combined assignment operator. A space cannot appear between the two operators that make a combined assignment operator.

·         Using a variable to receive the result of a calculation when the variable’s data type is incompatible with the data type of the result. A variable that receives the result of a calculation must be of a data type that is compatible with the data type of the result.

·         Incorrectly terminating a multi-line comment or a documentation comment. Multi-line comments and documentation comments are terminated by the */ characters. Forgetting to place these characters at a comment’s desired ending point, or accidentally switching the * and the /, will cause the comment not to have an ending point.

·         Forgetting to use the correct import statement in a program that uses the Scanner class or the JOptionPane class. In order for the Scanner class to be available to your program, you must have the import java.util.Scanner; statement near the top of your program file. In order for the JOptionPane class to be available to your program, you must have the import javax.swing.JOptionPane; statement near the top of the program file.

·         When using an input dialog to read numeric input, not converting the showInputDialog method’s return value to a number. The showInputDialog method always returns the user’s input as a string. If the user enters a numeric value, it must be converted to a number before it can be used in a math statement.

HINTS FOR CHAPTER 3

Read the following hints for correcting your JAVA programs

The following list describes several errors that are commonly committed when learning this chapter’s topics.

·         Using = instead of == to compare primitive values. Remember, = is the assignment operator and == tests for equality.

·         Using == instead of the equals method to compare String objects. You cannot use the == operator to compare the contents of a String object with another string. Instead you must use the equals or compareTo method.

·         Forgetting to enclose an if statement’s boolean expression in parentheses. Java requires that the boolean expression being tested by an if statement is enclosed in a set of parentheses. An error will result if you omit the parentheses or use any other grouping characters.

·         Writing a semicolon at the end of an if clause. When you write a semicolon at the end of an if clause, Java assumes that the conditionally executed statement is a null or empty statement.

·         Forgetting to enclose multiple conditionally executed statements in braces. Normally the if statement conditionally executes only one statement. To conditionally execute more than one statement, you must enclose them in braces.

·         Omitting the trailing else in an if-else-if statement. This is not a syntax error, but can lead to logical errors. If you omit the trailing else from an if-else-if statement, no code will be executed if none of the statement’s boolean expressions are true.

·         Not writing complete boolean expressions on both sides of a logical && or || operator. You must write a complete boolean expression on both sides of a logical && or || operator. For example, the expression x > 0 && < 10 is not valid because < 10 is not a complete expression. The expression should be written as x > 0 && x < 10.

·         Trying to perform case-insensitive string comparisons with the String class’s equals and compareTo methods. To perform case-insensitive string comparisons, use the String class’s equalsIgnoreCase and compareToIgnoreCase methods.

·         Using a SwitchExpression that is not an int, short, byte, char, or String. The switch statement can only evaluate expressions that are of the int, short, byte, char, or Stringdata types.

·         Using a CaseExpression that is not a literal or a final variable. Because the compiler must determine the value of a CaseExpression at compile time, CaseExpressions must be either literal values or final variables.

·         Forgetting to write a colon at the end of a case statement. A colon must appear after the CaseExpression in each case statement.

·         Forgetting to write a break statement in a case section. This is not a syntax error, but it can lead to logical errors. The program does not branch out of a switch statement until it reaches a break statement or the end of the switch statement.

·         Forgetting to write a default section in a switch statement. This is not a syntax error, but can lead to a logical error. If you omit the default section, no code will be executed if none of the CaseExpressions match the SwitchExpression.

·         Reversing the ? and the : when using the conditional operator. When using the conditional operator, the ? character appears first in the conditional expression, then the : character.

·         When formatting a number with System.out.printf or String.format, writing the flags, field width, and precision in an incorrect order.

·         When writing a format specifier for the System.out.printf or String.format methods, using the wrong type indicator (%f = floating-point, %d = integer, %s = string).

 

·        

ANSWERS TO MC

 

Chapter 1

Multiple Choice

1. C

2. D

3. A

4. B

5. A

6. C

7. B

8. D

 

Chapter 2

Multiple Choice

1. c

2. b

3. a

4. b and c

5. a, c, and d

6. a

7. c

8. b

9. a

10. d

11. b

12. a

13. a

14. c

15. a


Chapter 3

Multiple Choice

 

1. b

2. d

3. a

4. b

5. c

6. d

7. a

8. b

9. a

10. a

11. a

12. c

13. c

14. b

15. c