CPSC150 EH02 1

CPSC150 EH02

Q1: Which of the following is not a valid Java identifier?

  1. myValue
  2. $_AAA1
  3. width
  4. m_x

ANS:

Q2: Which of the following cannot cause a syntax error to be reported by the Java compiler?

a.Mismatched {}

b.Missing */ in a comment

c.Missing ;

d.Extra blank lines.

ANS:

Q3: Which of the following characters is the escape character?

a.*

b.\

c.\n

d.“

ANS:

Q4: Which of the following statements will print a single line containing “hello there”?

a.System.out.println ( "hello" );

System.out.println( " there" );

b.System.out.println ( "hello" , " there" );

c.System.out.println ( "hello" );

System.out.print ( " there" );

d.System.out.print ( "hello" );

System.out.println( " there" );

ANS:

Q5: Which of the following statement displays Hello World?

a.System.out.printf( “%2s”, “Hello ” “World”);

b.System.out.printf( “%s %s”, “Hello”, “World” );

c.System.out.printf( “%s%s”, “Hello, World” );

d.System.out.printf( “s% s%”, “Hello”, “World” );

ANS:

Q6

Run following “ComputeArea” program and submit your running result.

//e02_q06.java: ComputeArea

public class e02_q06{

/** Main method */

public static void main(String[] args) {

double radius; // Declare radius

double area; // Declare area

// Assign a radius

radius = 20; // New value in radius

// Compute area

area = radius * radius * 3.14159; // New value in area

// Display results

System.out.println("The area for the circle of radius " +

radius + " is " + area);

}

}

ANS:

Q7: Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following techniques:

a)Use one System.outprintln statement.

b)Use four System.out.print statements.

c)Use one System.out.printf statement.

ANS:

Q8:

Run following “ComputeChange” program and submit your running result.

// e02_q08.java: ComputeChange

import javax.swing.JOptionPane;

public class e02_q08{

/** Main method */

public static void main(String[] args) {

// Receive the amount

String amountString = JOptionPane.showInputDialog(

"Enter an amount in double, for example 11.56");

// Convert string to double

double amount = Double.parseDouble(amountString);

int remainingAmount = (int)(amount * 100);

// Find the number of one dollars

int numberOfOneDollars = remainingAmount / 100;

remainingAmount = remainingAmount % 100;

// Find the number of quarters in the remaining amount

int numberOfQuarters = remainingAmount / 25;

remainingAmount = remainingAmount % 25;

// Find the number of dimes in the remaining amount

int numberOfDimes = remainingAmount / 10;

remainingAmount = remainingAmount % 10;

// Find the number of nickels in the remaining amount

int numberOfNickels = remainingAmount / 5;

remainingAmount = remainingAmount % 5;

// Find the number of pennies in the remaining amount

int numberOfPennies = remainingAmount;

// Display results

String output = "Your amount " + amount + " consists of \n" +

numberOfOneDollars + " dollars\n" +

numberOfQuarters + " quarters\n" +

numberOfDimes + " dimes\n" +

numberOfNickels + " nickels\n" +

numberOfPennies + " pennies";

JOptionPane.showMessageDialog(null, output);

}

}

ANS:

Q9:

The following program can run without any difficulty. Please run it and then make some logic errors. Submit your running result(s) with some logic errors.

// e02_q09.java: Make Logic Error(s)

import javax.swing.JOptionPane;

public class e02_q09 {

// Determine if a number is between 1 and 100 inclusively

public static void main(String[] args) {

// Prompt the user to enter a number

String input = JOptionPane.showInputDialog(null,

"Please enter an integer:",

"ShowLogicErrors", JOptionPane.QUESTION_MESSAGE);

int number = Integer.parseInt(input);

// Display the result

System.out.println("The input number is: " + number);

System.out.println("Try 1: Using " + "((1 < number) & (number < 100))");

System.out.println("The number is between 1 and 100, " + "inclusively? " + ((1 < number) & (number < 100)));

}

}

ANS: