Computer Science Test Chap. 5, 6 Syntax, Integer Math, and Modulus
Name ___________________ Date __________
- Write output for the following code:
- public class IntMath {
- public static void main
(String args[]) {
-
int intAnswer, a = 1, b = 2, c = 3;
-
double doubleAnswer, x = 4, y = 5, z = 6;
-
-
intAnswer = a + b % b /c * 2 * a / c;
-
System.out.println("1) " +intAnswer);
- // 1) _________
-
-
intAnswer = (a + (b % b)) /((c * 2 * a) / c);
-
System.out.println("2) " +intAnswer);
- // 2) _________
-
-
intAnswer = ((a + b) % b /c) * (2 * (a / c));
-
System.out.println("3) " +intAnswer);
- // 3) _________
-
-
intAnswer = (int)y % b + (int)(x / z * b);
-
System.out.println("4) " +intAnswer);
- // 4) _________
-
-
intAnswer = (b / c)* (2* (a + (b + c)) / c);
-
System.out.println("5) " +intAnswer);
- // 5) _________
-
doubleAnswer = z * b / c;
-
System.out.println("6) " +doubleAnswer);
- // 6) _________
-
-
doubleAnswer = y % b + (int)(x / z * b)/(int)(z * z);
-
System.out.println("7) " +doubleAnswer);
- // 7) _________
-
-
doubleAnswer = (int)(y % b + (x / z * b)/(z * z));
-
System.out.println("8) " +doubleAnswer);
- // 8) _________
-
-
doubleAnswer = b * (z* x * y + 2 * z)/ z * (a /2);
-
System.out.println("9) " +doubleAnswer);
- // 9) _________
-
-
doubleAnswer = b * z* (double)(a/c);
-
System.out.println("10) " +doubleAnswer);
- //10) _________
- }
- }
-
- ______11. Which of the following is a literal
constant?
-
a) 4.7 b) Japplet c) static d) PI
e) none of the answers
-
- ______12. Which of the following is a symbolic
constant?
-
a) 4.7 b) Japplet c)
static d) PI e) all of the answers
-
- 13. Give an example of an escape sequence.
___________
14. Fill in all the blanks in the following table:
Data_Type |
Bytes |
byte |
|
boolean |
|
short |
|
|
|
integer |
|
long |
|
float |
|
double |
|
-
- 15. Correct the syntax errors on the
attached program
- Import java.awt.*;
- import java.awt.event*;
- import Javax.swing.*;
- public class Banner inherits applet;
implements ActionListener;{
- private int xPos, yPos;
- /**
- * Called
automatically when the applet is initialized
- *\
- public void main
-
- Container c =
getContentPane
-
-
c.setBackground(Color.white);
-
- xPos = c.getWidth();
-
- yPos + 7 =
c.getHeight() / 2;
-
- Timer clock = new
Timer(30, this) \\ Fires every 30 milliseconds
-
- clock.start();
- }
- /
- * Called
automatically after a repaint request
- */
-
- public void paint(Graphics g)
- {
- super.paint(g);
-
- 2g.drawString(Hello,
World, xPos, yPos);
-
- public Void
actionPerformed(ActionEvent e){
- Container c =
getContentPane();
-
- xPos--;
-
- if (xPos < -100){
- xPos =
C.getWidth();
- }
-
- yPos = C.getHeight()
/ 2;
-
- repaint();
- }
-
- ______16. Which of the following is a literal constant?
-
a) 4.7 b) Japplet c) static d) PI
e) none of the answers
-
- ______17. Which of the following is a symbolic constant?
-
a) 4.7 b) Japplet c) static d) PI
e) all of the answers
-
- 18. Give an example of an escape sequence. ___________
-
- 19. Write a software on-off switch which turns on (changes
from zero to one) at an integer value of 7
-
- 20. Write a short program which uses command line
input to input an integer value and test to see
- if it is odd or even. The program
will output a 1 if odd and a 0 if even.
-
- 21. Write a short code segment which takes a double
called dollars and rounds tenths
- of cents down. In other words,
eliminates 1/10 cents.