Sie sind auf Seite 1von 18

Part 1: Fill in the Blank ( all are 11, you can choose 10)

1. In a computer system, the ________System software_______ _______________ [two words] forms a


layer between the hardware and the applications.

2. How are numbers stored inside a computer?

stored as groupings of bits, named for the number of bits that compose them.

Ex: 0 = Yes 1 = NO

3. Inside a computer system, which of the following kinds of data are stored in binary: (a) integers, (b)
floating-point numbers, (c) characters, or (d) all of the above?

d. all of the above

4. The native language of a particular CPU is called ________Machine____ language.

5. A Java program must be _____translated into machine language __________ (converted to bytecode
instructions) before it can be executed.

6. What is the final step in the programming process, after compiling and executing the program _
Loader____________?

7. In order to run a Java program named Foo, which file(s) would someone need to have: (a) Foo.java,
(b) Foo.class, or (c) both Foo.java and Foo.class.

Answ:.Foo.class

8. Which one of the following is not a legal comment in Java?

It is (e)

(a) /* This is a comment */


(b) /* This is a comment **/

1
(c) /** This is a comment */
(d) // This is a comment
(e) None of the above( Right answer)

9. Every variable in a Java program must have a __data type_____________, which identifies the kind of
data that it will store.

10. 125, 3.14, false, and 'z' are all examples of _______________.

11. Which one of the following is not a legal identifier: (a) b_l_u_e___v_e_l_v_e_t, (b) 7_Samurai, (c)
Apollo_13_, or (d) _PULP_FICTION?

(b) ) 7_Samurai

Part 2: Problem Solving (all are 22, you can choose only 20.)

1. Write a variable declaration that declares the variable emergency to be an integer and assigns it the
initial value 911.

var i:int;

i = 911;

2. Add parentheses to the following expressions to indicate how Java will interpret them.

(a) a * b * c - d / e

((a * b) * c) - (d / e)

(b) a + - b * c d

(a + ((-b) * c)) d

(c) a - b - c * d / e
(( a b) c) *( d / e)

(d) (a % (b / c ))% (d + e)
(e) ((a / b) +( c d) * e)

2
3. Show how the following statement could be shortened:

n = n * 3;

4. What will be printed when the following statements are executed?

Answer is : 3.7

double a = 4.5, b = 3.7, c = 8.1;

double result = Math.min(a, Math.min(b, c));

System.out.println(result);

5. Show the output produced by the following statement:

1"2\3

System.out.print("1\"2\\3");

6. Suppose that Point is a class whose instances represent points in two-dimensional space. Assume that
Point has two instance variables named x and y. What is wrong with the following constructor for the
Point class?

public void Point() {

x = 0;

y = 0;

7. Write a statement that creates an Account object containing $500.00 and saves it in a variable named
acct.

3
Account acct = acct (500.00);

8. Let Account be the bank account class discussed in Chapter 3.ppt. What balance will be stored in
acct1, acct2, and acct3 after the following statements have been executed?

Ans: Balance in acct1: 250.00. Balance in acct2: 250.00. Balance in acct3: 250.00.

Account acct1 = new Account(100.00);

Account acct2 = acct1;

Account acct3 = new Account(200.00);

acct1.withdraw(50.00);

acct2.deposit(100.00);

acct3.deposit(50.00);

9. After the following statements have been executed, how many Fraction objects will exist, not
counting garbage objects? Answer is(One)

Fraction f1 = new Fraction(1, 2);

Fraction f2 = new Fraction(3, 5);

Fraction f3 = f2;

f2 = null;

f1 = f2;

10. What is the Output for following code fragment?

Ans: Hawaii: 5 0

4
System.out.println("Hawaii: " + 5 +" "+ 0);

11. Suppose that f1 and f2 are Fraction objects, where Fraction is the class described in Chapter 3. Write
a statement that adds f1 and f2 and stores the result in a variable named f3. (The method that adds
fractions is named add.) You may assume that f3 has already been declared as a Fraction variable.

Answ: Fraction f3 = f1.add(f2);

12. The following questions refer to the class shown below.

class Thermometer {

private int temperature;

public Thermometer(int degrees) {

temperature = degrees;

public Thermometer() {

temperature = 0;

public void makeWarmer(int degrees) {

temperature += degrees;

public void makeCooler(int degrees) {

5
temperature -= degrees;

public int getTemperature() {

return temperature;

public String toString() {

return temperature + " degrees";

How many constructors does this class have?

It is 2

How many methods does this class have? (Constructors don't count.)

It is 4

Write a declaration that declares a Thermometer variable named t and initializes it to contain a
Thermometer object representing 32 degrees.

Thermometer t = new Thermometer (32);

Write a statement that increases the temperature stored in the t object by 10 degrees.

t.Makewarmer =(10);

(e) What are "get" methods such as getTemperature known as?

Accessors , Getters

(f) If t is the Temperature variable described in part (c), what does the following statement display on
the screen System.out.println(t);

32 degrees

6
13. Write 3-5 lines of Java code that will request and obtain a value for an integer variable from a user.
(Hint: Your lines should consist of a declaration, a prompt, and an assignment.)

Int number.grade;

Simple0.prompt[enter name (0-100););

String userInput = simple0.read.line

Grade =integer.parseTnt(userinput);

14. Show the output of the following program:

class Blue_ManTest{

public static void main(String[] args){

String name = "I LOVE JAVAWORLD";

int index1 = name.indexOf(" ");

int index2 = name.lastIndexOf(" ");

String str1 = name.substring(0, index1);

String str2 = name.substring(index1 + 1, index1+ 5);

String str3 = name.substring(index2 + 5);

System.out.println(str3 + ", " + str1 + " " + str2 + ".");

Ans: ____________________ WORLD, I LOVE._____________________________________

15. Some keywords cant be used as identifiers because Java has already given them a meaning. Please
find these keywords from the following words:

Ans: They, we, you,

7
.

They, Double, int, super, else, interface, switch, we, long, synchronized, you,
byte, final, native, this, new, throw, catch, float, package, throws,
private, transient, class, goto, try, Const, if, public, void, implements, return,
volatile, short, while, instanceof, true, goHome

16. CourseAverage.java

// Program name: CourseAverage

// Author: K. N. King

// Written: 1998-04-05

// Modified: 1999-01-27

//

// Prompts the user to enter eight program scores (0-20), five

// quiz scores (0-10), two test scores (0-100), and a final

// exam score (0-100). Scores may contain digits after the

// decimal point. Input is not checked for validity. Displays

// the course average, computed using the following formula:

//

// Programs 30%

// Quizzes 10%

// Test 1 15%

// Test 2 15%

// Final exam 30%

//

// The course average is rounded to the nearest integer.

8
____________________ Public class_________________ CourseAverage {

______________________ Public static void main(String[] args) {

// Print the introductory message

_____________________________________ System.out.println ("Welcome to the CSc 2310


average " +

"calculation program.\n");

// Prompt the user to enter eight program scores

SimpleIO.prompt("Enter Program 1 score: ");

___________________________String userInput = SimpleIO.readLine();

_________________________Double__ program1 = Convert.toDouble(userInput);

// Round the course average to the nearest integer and

// display it

System.out.println__________________________ ("\nCourse average: " +

Math.round(courseAverage));

17. The VIN program will split a VIN into its constituent pieces. The VIN is entered by the user when
prompted:

Enter VIN: JHMCB7658LC056658, Manufacturer identifier: JHM, Vehicle description:


CB765

Check digit: 8, Vehicle identification is the rest.

9
Here we have the following Java code, and what is the output for this code?

VIN2.java

// Displays information from a VIN entered by the user

import jpb.*;

public class VIN2 {

public static void main(String[] args) {

// Prompt the user to enter a VIN

SimpleIO.prompt("Enter VIN: ");

String vin = SimpleIO.readLine();

// Display the parts of the VIN

System.out.println("World manufacturer identifier: " +

vin.substring(0, 3));

System.out.println("Vehicle description section: " +

vin.substring(3, 8));

System.out.println("Check digit: " + vin.substring(8, 9));

System.out.println("Vehicle identification section: " +

vin.substring(9));

10
18. Find and correct the error(s) in each of the following segments of code:

a) For ( i = 100, i >= 1, i++ )

System.out.println( i );

ANS:

The F should be lowercase. Semicolons should be used in the for header instead of commas. ++ should
be --.

b) The following code should print whether integer value is odd or even:

switch ( value % 2 )

case 0:

System.out.println( "Even integer" );

case 1:

System.out.println( "Odd integer" );

ANS: Breaks statement should be placed in case 0:

c) The following code should output the odd integers from 19 to 1:

11
for ( i = 19; i >= 1; i += 2 )

System.out.println( i );

ANS:

The operator in the for header should be -=.

d) The following code should output the even integers from 2 to 100:

counter = 2;

do

System.out.println( counter );

counter += 2;

} While ( counter < 100 );

ANS:

The w in while should be lowercase. < should be <=.

19. What does the following program do?

Answ: Its 4 loop which plays double statements the char 5 times(@) will display around 10 rows on
the output

12
20. Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?

a) System.out.println( i == 1 );

ANS: True

b) System.out.println( j == 3 );

ANS: False

c) System.out.println( ( i >= 1 ) && ( j < 4 ) );

ANS: True

d) System.out.println( ( m <= 99 ) & ( k < m ) );

ANS: False

e) System.out.println( ( j >= i ) || ( k == m ) );

ANS: True

13
f) System.out.println( ( k + m < j ) | ( 3 - j >= k ) );

ANS: False

g) System.out.println( !( k > m ) );

ANS: False

21. (Calculating the Value of ) Calculate the value of from the infinite series

Answ:

int num;

printf("pi will be calculated using the infinite series: \n"

"pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...\n"

scanf("%d", &num);

if ( num > 0)

{ pi_table(num);

} else

{ printf("invalid input... using default... term=10...\n");

pi_table(10); } return 0; } /* pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...

Print a table that shows the value of pi by n terms of this series.

*/ void pi_table(int n)

{ int i; double pi=0.0, temp;

14
for (i=0;i<n;i++)

{ temp = 4.0 / (2 * i + 1);

if ((i%2)==1)

{ temp *= -1; }

pi += temp;

printf("term %10d ------ pi = %.10f\n", i+1, pi); }}

Print a table that shows the value of approximated by computing the first 200,000 terms of this

series. The possible code is as follows, it is right? IF not, why? IF yes, what is the output (only show the
first 3 lines).

public class Pi

public static void main( String[] args )

double piValue = 0; // current approximation of pi

double number = 4.0; // numerator of fraction

double denominator = 1.0; // denominator

int accuracy = 200000; // maximum number of terms in series

System.out.printf( "Accuracy: %d\n\n", accuracy );

15
System.out.println( "Term\t\tPi" );

for ( int term = 1; term <= accuracy; term++ )

if ( term % 2 != 0 )

piValue += number / denominator;

else

piValue -= number / denominator;

System.out.printf( "%d\t\t%.16f\n", term, piValue);

denominator += 2.0;

} // end for loop

} // end main

} // end class Pi

Ans:

22. What does the following program segment do?

for ( i = 1; i <= 5; i++ )

for ( j = 1; j <= 3; j++ )

for ( k = 1; k <= 4; k++ )

System.out.print( '*' );

System.out.println();

16
} // end inner for

System.out.println();

} // end outer for

Ans:

23. Write an application that finds the smallest of several integers. Assume the first value read specifies
the number of values to input from the user.

import java.util.Scanner;

public class Small

// finds the smallest integer

public static void main( String[] args )

Scanner input = new Scanner( System.in );

int smallest = 0; // smallest number

int number = 0; // number entered by user

int integers; // number of integers

System.out.print( "Enter number of integers: " );

integers = input.nextInt();

17
for ( int counter = 1; counter ________; _____________ )

System.out.print( "Enter integer: " );

number = input.nextInt();

if ( counter == 1 )

_____________________________;

else if ( number < smallest )

_____________________________;

} // end for loop

System.out.printf( "Smallest Integer is: %d\n", smallest );

} // end main

} // end class Small

18

Das könnte Ihnen auch gefallen