computer

1. In a computer system, the _______________ _______________ [two words] forms a layer between the hardware and the applications.

2. How are numbers stored inside a computer

Don't use plagiarized sources. Get Your Custom Essay on
computer
Just from $13/Page
Order Essay

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

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

5. A Java program must be _______________ (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 _____________

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.

8. Which one of the following is not a legal comment in Java
(a) /* This is a comment */
(b) /* This is a comment **/
(c) /** This is a comment */
(d) // This is a comment
(e) None of the above
9. Every variable in a Java program must have a _______________, 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

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.

2. Add parentheses to the following expressions to indicate how Java will interpret them.
(a) a * b * c – d / e
(b) a + – b * c – d
(c) a – b – c * d / e
(d) a % b / c % d + e
(e) a / b + c – d * e

3. Show how the following statement could be shortened:
n = n * 3;

4. What will be printed when the following statements are executed
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:
System.out.print(“1″23”);

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.

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
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
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
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.

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) {
temperature -= degrees;
}

public int getTemperature() {
return temperature;
}

public String toString() {
return temperature + ” degrees”;
}
}
(a) How many constructors does this class have
(b) How many methods does this class have (Constructors don’t count.)
(c) Write a declaration that declares a Thermometer variable named t and initializes it to contain a Thermometer object representing 32 degrees.
(d) Write a statement that increases the temperature stored in the t object by 10 degrees.
(e) What are “get” methods such as getTemperature known as
(f) If t is the Temperature variable described in part (c), what does the following statement display on the screen System.out.println(t);

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.)

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: _________________________________________________________

15. Some keywords cant be used as identifiers because Java has already given them a meaning. Please find these keywords from the following words:
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.
_____________________________________ CourseAverage {
_______________________________________ void main(String[] args) {
// Print the introductory message
_____________________________________.println(“Welcome to the CSc 2310 average ” +
“calculation program.n”);

// Prompt the user to enter eight program scores
SimpleIO.prompt(“Enter Program 1 score: “);
___________________________ userInput = SimpleIO.readLine();
___________________________ program1 = Convert.toDouble(userInput);

// Round the course average to the nearest integer and
// display it
System.__________________________ (“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.
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));
}
}

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:

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:

c) The following code should output the odd integers from 19 to 1:
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;
} While ( counter < 100 ); ANS: 19. What does the following program do
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:

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

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

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

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

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

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

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

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: %dnn”, accuracy );
System.out.println( “TermttPi” );

for ( int term = 1; term <= accuracy; term++ )
{
if ( term % 2 != 0 )
piValue += number / denominator;
else
piValue -= number / denominator;

System.out.printf( “%dtt%.16fn”, 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();
} // 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();

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: %dn”, smallest );
} // end main
} // end class Small