Beruflich Dokumente
Kultur Dokumente
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?
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
It is (e)
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;
Answer is : 3.7
System.out.println(result);
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?
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.
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 f3 = f2;
f2 = null;
f1 = f2;
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.
class Thermometer {
temperature = degrees;
public Thermometer() {
temperature = 0;
temperature += degrees;
5
temperature -= degrees;
return temperature;
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.
Write a statement that increases the temperature stored in the t object by 10 degrees.
t.Makewarmer =(10);
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;
Grade =integer.parseTnt(userinput);
class Blue_ManTest{
15. Some keywords cant be used as identifiers because Java has already given them a meaning. Please
find these keywords from the following words:
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
// Author: K. N. King
// Written: 1998-04-05
// Modified: 1999-01-27
//
//
// Programs 30%
// Quizzes 10%
// Test 1 15%
// Test 2 15%
//
8
____________________ Public class_________________ CourseAverage {
"calculation program.\n");
// display it
Math.round(courseAverage));
17. The VIN program will split a VIN into its constituent pieces. The VIN is entered by the user when
prompted:
9
Here we have the following Java code, and what is the output for this code?
VIN2.java
import jpb.*;
vin.substring(0, 3));
vin.substring(3, 8));
vin.substring(9));
10
18. Find and correct the error(s) in each of the following segments of code:
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:
case 1:
11
for ( i = 19; i >= 1; i += 2 )
System.out.println( i );
ANS:
d) The following code should output the even integers from 2 to 100:
counter = 2;
do
System.out.println( counter );
counter += 2;
ANS:
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
ANS: True
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;
scanf("%d", &num);
if ( num > 0)
{ pi_table(num);
} else
*/ void pi_table(int n)
14
for (i=0;i<n;i++)
if ((i%2)==1)
{ temp *= -1; }
pi += temp;
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
15
System.out.println( "Term\t\tPi" );
if ( term % 2 != 0 )
else
denominator += 2.0;
} // end main
} // end class Pi
Ans:
System.out.print( '*' );
System.out.println();
16
} // end inner for
System.out.println();
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;
integers = input.nextInt();
17
for ( int counter = 1; counter ________; _____________ )
number = input.nextInt();
if ( counter == 1 )
_____________________________;
_____________________________;
} // end main
18