Orange Questions

 

Java Lesson 2

1. State whether each of the following is true or false, explain why.

    a) Comments cause the computer to print the text after the // on the screen when the program is executed.

    b) All variables must be given a type when they are declared.

    c) Java considers the variables number and NuMbEr to be identical.

    d) The modulus operator (%) can be used only with integer operands.

    e) The arithmetic operators *, /, %, + and - all have the same level of precedence.

    f) Method Integer.parseInt converts an integer to a String.

 

2. Write Java statements to accomplish each of the following:

    a) Declare variables c, thisIsAVariable, q76354 and number to be of type int.

    b) Display a dialog asking the user to enter an integer.

    c) Convert a String to an integer and store the converted value in integer variable age.

    d) If the variable number is not equal to 7, display "The variable number is not equal to 7" in a message dialog.

    e) Print the message "This is a Java program" on one line in the command window.

    f) Print the message "This is a Java program" on two lines in the command window where the first line ends with Java. Use only one statement.

 

3. Identify and correct the errors in each of the following statements:

    a) if (c < 7);

            JOptionPane.showMessageDialog( null, "c is less than 7" );

    b) if (c => 7 )

            JOptionPane.showMessageDialog(null, "c is equal to or greater than 7");

 

4. Write a statement (or comment) to accomplish each of the following:

    a) State that a program will calculate the product of three integers.

    b) Declare the variables x, y, z and result to be of type int.

    c) Declare the variables xVal, yVal and zVal to be of type String.

    d) Prompt the user to enter the first value, read the value from the user and store it in the variable xVal.

    e) Prompt the user to enter the second value, read the value from the user and store it in the variable yVal.

    f) Prompt the user to enter the third value, read the value from the user and store it in the variable zVal.

    g) Convert xVal to an int and store the result in the variable x.

    h) Convert yVal to an int and store the result in the variable y.

   i) Convert zVal to an int and store the result in the variable z.

    j) Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result.

    k) Display a dialog "The product is " followed by the value of the variable result.

    l) Return a value from main indicating that the program terminated successfully.

 

5. Using the statements you wrote in question 4, write a complete program that calculates and prints the product of three integers.

 

6. Write Java statements that accomplish each of the following:

    a) Display the message "Enter two numbers" using class JOptionPane.

    b) Assign the product of variables b and c to variable a.

    c) State that a program performs a sample payroll calculation (i.e., use text that helps to document a program).

 

7. State whether each of the following is true or false. If false, explain why.

    a) Java operators are evaluated from left to right.

    b) The following are  all valid variable names: _under_bar, m928134, t5, j7, her_sales$, his_$account_total, a, b$, c, z, z2.

    c) A valid Java arithmetic expression with no parenthesis is evaluated from left to right.

    d) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.

 

8. Fill in the blanks in each of the following:

    a) What arithmetic operations have the same precedence as multiplication? ________________

    b) When parentheses are nested, which set of parenthesis is evaluated first in an arithmetic expression? ______________

    c) A location in the computer's memory that may contain different values at various times throughout the execution of a program is called a _________________________.

 

9. What displays in the message dialog when each of the following Java statements is preformed? Assume x = 2 and y = 3.

    a) JOptionPane.showMessageDialog(null, "x = " + x);

    b) JOptionPane.showMessageDialog(null, "The value of x + x is  " + (x + x));

    c) JOptionPane.showMessageDialog(null, "x = " );

    d) JOptionPane.showMessageDialog(null, (x + y) + " = " + (y + x));

 

10. Which of the following Java statements contain variables whose values are destroyed (i. e., changed or replaced)?

    a) p = i + j + k + 7;

    b) JOptionPane.showMessageDialog(null, "variables whose values are destroyed");

    c) JOptionPane.showMessageDialog(null, "a = 5" );

    d) stringVal = JOptionPane.showInputDialog("Enter string: " );

 

11. Given y = ax3 + 7, which of the following are correct statements for this equation?

    a) y = a * x * x * x + 7;

    b) y = a * x * x * (x + 7);

    c) y = (a * x) * x * (x + 7);

    d) y = (a * x) * x * x +7;

    e) y = a * (x * x * x) + 7;

    f) y = a * x * (x * x + 7);

 

12. State the order of evaluation of the operators in each of the following Java statements and show the value of x after each statement is performed.

    a) x = 7 + 3 * 6 / 2 - 1;

    b) x = 2 % 2 + 2 * 2 - 2 / 2;

    c) x = ( x * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );

 

13. Write an application that displays in the command window a box, an oval, an arrow and a diamond using asterisks (*) as follows:

 

*********           ***                 *                     *

*       *         *     *              ***                   * *

*       *        *       *            *****                 *   *

*       *        *       *              *                  *     *

*       *        *       *              *                 *       *

*       *        *       *              *                  *     *

*       *         *     *               *                    * *

*********           ***                 *                     *

 

14. Modify the program you created in question 13 to display the shapers in a JOptionPane.PLAIN_MESSAGE dialog. Does the program display the shapes exactly as in question 13?

 

15. What does the following code print?

    System.out.println("*\n**\n***\n****\n*****");

 

16. What does the following code print?

 

    System.out.println("*");

    System.out.println("***");

    System.out.println("*****");

    System.out.println("****");

    System.out.println("**");

 

17. What does the following code print?

 

    System.out.print("*");

    System.out.print("***");

    System.out.print("*****");

    System.out.print("****");

    System.out.println("**");

 

18. Write an application that displays in a message box a checkerboard pattern as follows:

 

* * * * * * * *

 * * * * * * * *

* * * * * * * *

 * * * * * * * *

* * * * * * * *

 * * * * * * * *

* * * * * * * *

 * * * * * * * *

 

19. Modify the program you wrote in question 18 to display the checkerboard pattern in a JOptionPane.PLAIN_MESSAGE dialog. Does the program display the shapes exactly as in question 18?

 

20. Using only the programming techniques you learned in Lesson 2, write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format as follows:

 

number    square    cube

0             0            0

1              1            1

2             4            8

3             9            27

4             16          64

5             25          125

6             36          216

7             49          343

8             64          512

9             81           729

10            100        1000

 

21. Write a program that reads a first name and a last name from the user as two separate inputs and concatenates the first name and last name separated by a space. Display in a message dialog the concatenated name.