Академический Документы
Профессиональный Документы
Культура Документы
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;
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_;
f = 23.4210
1
float f, q;
q = f (long)f;
byte b;
short s;
short i;
i = b + s;
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.
Possible choices:
a) Computer programming.
b) input unit.
c) Assembly language.
d) The memory unit
e) The output unit.
f) the secondary storage unit.
a) ____ The arithmetic and logic unit (ALU)_ is a logical unit of the
2
computer that performs calculations.
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
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.
while ( x <= 10 )
{
sum += x;
++x;
}
6
Output:
Output:
7
16. What does the following program print?
Output:
8
17. What does the following program segment do?
Output:
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.
} // 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.
} // 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.
13
double small = w[ 0 ];
double large = w[ 0 ];
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;
Output:
15
// Program creates a spiral.
import javax.swing.JFrame;
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
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;
Figure 1: Spiral drawn using method drawPolyline, and the upper left corner has the title Spiral
of this frame
17