Вы находитесь на странице: 1из 17

PART 1:

1. Explain what's wrong with the following code and show how to fix it.

int i = 1;
while (i <= 10)
System.out.println("The square of " + i + " is " + i * i);

i++;

int i = 1;

while (i <= 10)


{
System.out.println("The square of " + i + " is " + i * i);

i++;
}
}
}

2. What will be printed when the following statements are executed (3 questions have
relationship)?

i = 6;
a. System.out.println(i--);
b. System.out.println(++i);
c. System.out.println(i);

a. is ___6__;
b. is ____6__;
c. is ____6__;

3. For the above Question 2, If they are 3 different questions (no relationship), then what are the
results?

a. is ___6;
b. is ___7_;
c. is ___6_;

4. what is the value for q?

f = 23.4210

1
float f, q;
q = f (long)f;

5. the following code is right?

byte b;
short s;
short i;
i = b + s;

6. Fill in the blanks using the following possible choices:

a) The logical unit of the computer that receives information from


outside the computer for use by the computer is the ___ input
unit______.

b) The process of instructing the computer to solve a problem is called


__ Computer programming.

c) __ Assembly language__ is a type of computer language that uses


English like abbreviations for ma chine-language instructions.

d) ____ The output unit_ is a logical unit of the computer that sends
information which has already been processed by the computer to
various devices so that it may be used outside the computer.

e) __ The memory unit_ and _ the secondary storage unit._ are


logical units of the computer that retain information.

Possible choices:
a) Computer programming.
b) input unit.
c) Assembly language.
d) The memory unit
e) The output unit.
f) the secondary storage unit.

7. Fill in the blanks using the following possible choices:

a) ____ The arithmetic and logic unit (ALU)_ is a logical unit of the

2
computer that performs calculations.

b) __ The arithmetic and logic unit (ALU)_is a logical unit of the


computer that makes logical decisions.

c) _high-level languages are most convenient to the programmer for


writing programs quickly and easily.

d) The only language a computer can directly understand is that


computers _Machine language_.

e) _ The central processing unit (CPU) is a logical unit of the


computer that coordinates the activities of all the other logical units.

f) _____Java___is now used to develop large-scale enterprise


applications, to enhance the functionality of web servers, to provide
applications for consumer devices and for many other purposes.

Possible choices:

a) The arithmetic and logic unit (ALU).


b) The arithmetic and logic unit (ALU).
c) Machine language.
d) High-level.
e) The central processing unit (CPU).
f) Java.

8. Fill in the blanks using the following possible choices:

a) A(n) ___ integrated development environment (IDE) provides many tools that support the
software development process,
such as editors for writing and editing programs, debuggers for locating logic errors in
programs, and many other features.

b) The command java invokes the Java Virtual Machine (JVM)__, which executes Java
programs.

c) A(n) _ virtual machine (VM) is a software application that simulates a computer, but hides the
underlying operating system and hardware from the programs that interact with it.

d) The __ class loader_ takes the .class files containing the programs bytecodes and transfers
them to primary memory.

e) The __ bytecode verifier_ examines bytecodes to ensure that they are valid.

Possible choices:
a) virtual machine (VM).
b) bytecode verifier.

3
c) integrated development environment (IDE).
d) class loader.
e) Java Virtual Machine (JVM).

9. Given that , which of the following are correct Java 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 );

10. 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;
*, /, +, -; Value of x is 15.

b) x = 2 % 2 + 2 * 2 - 2 / 2;
%, *, /, +, -; Value of x is 3.

c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
4 5 3 1 2 Value of x is 324.

PART 2:

11. For the following code, if we have 3 input number: 10, 20, 30, what is the final result:

4
Output
Enter first integer: 10
Enter second integer: 20
Enter third integer: 30
Product is 6000

12. What does the following code print?


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

Output:
*
**
***
****
*****
Screenshot:

5
13. Write a Java application that calculates and prints the sum of the integers from 1 to 10. Use a
while statement to loop through the calculation and increment statements. The loop should
terminate when the value of x becomes 11.

// Calculate the sum of the integers from 1 to 10


public class Calculate
{
public static void main( String[] args )
{
int sum;
int x;

x = 1; // initialize x to 1 for counting


sum = 0; // initialize sum to 0 for totaling

while ( x <= 10 )
{

sum += x;
++x;
}

System.out.printf( "The sum is: %d\n", sum );


} // end main
} // end class Calculate

14. What does the following program print?

6
Output:

15. What does the following program print?

Output:

7
16. What does the following program print?

Output:

8
17. What does the following program segment do?

Output:

18. What does the following program segment do?

9
Output:

10
19. For the following code, if the input is 4, then what does the following program segments do?

11
Output:

12
20. Write Java statements to accomplish the following tasks: Total the 100 elements of floating-
point array c.

public class Parts


{
public static void main( String args[] )
{
double c[] = new double[ 100 ];
double total = 0;

for ( int k = 0; k < c.length; k++ )


total += c[ k ];

} // end main
} // end class Parts

21. Write Java statements to accomplish the following tasks: Copy 11-element array a into the
first portion of array b, which contains 34 elements.

public class Parts


{
public static void main( String args[] )
{
double a[] = new double[ 11 ];
double b[] = new double[ 34 ];

for ( int j = 0; j < a.length; j++ )


b[ j ] = a[ j ];

} // end main
} // end class Parts

22. Write Java statements to accomplish the following tasks: Determine and display the smallest
and largest values contained in 99-element floatingpoint array w.

public class Parts


{
public static void main( String args[] )
{
double w[] = new double[ 99 ];

13
double small = w[ 0 ];
double large = w[ 0 ];

for ( int i = 0; i < w.length; i++ )


if ( w[ i ] < small )
small = w[ i ];
else if ( w[ i ] > large )
large = w[ i ];

System.out.printf( "%f %f\n", small, large );


} // end main
} // end class

23. Write the Java code for the following graph, using graphics method drawPolyline to draw a
spiral similar to the following graph (Figure 1). We have some codes as followings. Please check
them. It is right or Wrong? Why? IF you think it is almost right, please fill in some codes.

I checked the code and I think it is wrong some methods are not completed

14
Code:
import javax.swing.JFrame;
import java.awt.Graphics;

import javax.swing.JPanel;

public class CircSpiral extends JPanel {

public void paintComponent(Graphics g) {


int x = getSize().width / 2 - 10;
int y = getSize().height/ 2 - 10;
int width = 20;
int height = 20;
int startAngle = 0;
int arcAngle = 180;
int depth = 10;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
y = y - depth;
width = width + 2 * depth;
height = height + 2 * depth;
g.drawArc(x, y, width, height, startAngle, -arcAngle);
} else {
x = x - 2 * depth;
y = y - depth;
width = width + 2 * depth;
height = height + 2 * depth;
g.drawArc(x, y, width, height, startAngle, arcAngle);
}
}
}

public static void main(String[] args) {


CircSpiral panel = new CircSpiral();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300, 300);
application.setVisible(true);
}
}

Output:

15
// Program creates a spiral.
import javax.swing.JFrame;

public class Spiral


{
public static void main( String args[] )
{
// create frame for SpiralJPanel
JFrame frame = new JFrame( "___________________" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

SpiralJPanel spiralJPanel = new SpiralJPanel();


frame.add( spiralJPanel ); // add spiralJPanel to frame
frame.setSize( 300, 330 ); // set frame size
frame.setVisible( true ); // display frame
} // end main
} // end class Spiral

// Program creates a spiral.


import java.awt.Graphics;
import javax.swing.JPanel;

public class SpiralJPanel extends JPanel


{
private static final int POINTS = 19; // number of points to render
private int[] x; // set of x-coordinates
private int[] y; // set of y-coordinates

// parameters for spiral size


private static final double RADIAN_STEP = 0.25 * Math.PI;
private static final int DISTANCE_STEP = 5;
private static final int DISTANCE_MIN = 30;
private static final int CENTER = 150;

public SpiralJPanel( )
{
// create arrays to hold coordinates
x = new int[ POINTS ];
y = new int[ POINTS ];

// initialize arrays
for ( int i = 0; i < POINTS; ++i )
{
// calculate amount of rotation and distance from center

16
double radians = i * RADIAN_STEP;
int distance = DISTANCE_MIN + DISTANCE_STEP * i;

// calculate x- and y-coordinates


x[ i ] = ( int )( CENTER + distance * Math.cos( radians ) );
y[ i ] = ( int )( CENTER + distance * Math.sin( radians ) );
} // end for
}

public void paintComponent( Graphics g )


{
super.paintComponent( g );

g.drawPolyline( ____________________ ); // draw spiral to screen

} // end method paintComponent

} // end class SpiralJPanel

Figure 1: Spiral drawn using method drawPolyline, and the upper left corner has the title Spiral
of this frame

17

Вам также может понравиться