Intro to Java Programming
Prof. Gregory Safko

Camden County College

Review: Exam 1


 

General Knowledge

1.   Know how to write Welcome.java from memory

2.   Know the difference between a .java file and a .class file

3.   Know the major differences between Java programs and Java applets

4.   Know the difference between a compiler program and an interpreted program, and why Java can be considered a program that contains both behaviors.

5.   Know the 2 ways to write comments

6.   Know the import command and why it is needed

7.   Know the extends command and why it is needed

8.   Know how to use command line arguments (String args[ ])

9.   Know how to use the SafkoInput class

10. Know the two methods of the JOptionPane object that we discussed in class

11. Know the difference between and when to use Integer.parseInt( ) and Double.parseDouble( )

12. Know when to put a semi-colon ( ; ) at the end of statements

13. Know the difference between the .println( ) method and the .print( ) method

14. Know how to typecast integers to doubles, and vice verse

15. Know the difference between = and ==, and why the statement if(a = 1) is a compiler error

16. Know why you would use the keyword final

17. Know the four parameters used in the JOptionPane.showMessageDialog( ) method

18. Know the two parameters used in the JOptionPane.showInputDialog( ) method

19. Know why you need System.exit( 0 );   when ending programs that use JOptionPane

20. What are the 6 comparison operators?

21. What is the proper way to use a while loop? A do-while loop?

22. What is the proper way to use the if statement? The if-else statement?

23. Know the use and difference between the pre- and post- increment and decrement operators (++a, a++, --a, a--)

 

 

Multiple Choice

Consider the following question:

 

Java is very similar to _____ because they are both _____ languages

a.      BASIC, interpreted

b.      BASIC, compiled

c.      C++, interpreted

d.      C++, compiled

 

There are two correct answers to this question (a and d). Can you explain why?

 

 

Sample Programs

 

Write a program that will take two numbers and will find their average.

Write it using command line arguments, as follows:

Write it using SafkoInput, as follows:

Write it using JOptionPane, as follows:

 

 

What would be the problem if you entered a 3 and 4, and instead of displaying 3.5, it displays 3?

 

 

Debugging

Correct what is wrong with the following (there may be more than one thing wrong). Assume the all the appropriate imports and variables have been declared.

 

1.  // This will test if a number equals 0

int x;

x = SafkoInput.readInt();

if (x = 0)

  System.out.println(“It’s zero");

 

 

2.  // This will test if a number is negative

int a;

a = SafkoInput.readInt();

if a < 0

  System.out.println(“It’s negative");

else

System.out.println(“It’s positive");

 

 

3.  // This will test if a number is negative

int a;

a = SafkoInput.readInt();

if (a < 0);

  System.out.println(“It’s negative");

else

System.out.println(“It’s positive");

else

System.out.println(“I can’t tell");

 

4.  // This code will loop while k not equal to zero

int b,k;

b = SafkoInput.readInt();

k = b/2;

while k != 0

{

if k = b

System.out.println(“It’s even");

else

System.out.println(“It’s odd");

}

 

5.  // This code will test if m is even

int m,p;

p = m/2;

System.out.print(“Enter a number: ");

m = SafkoInput.readInt();

if (m = 2 * p)

System.out.println(m + “ is even");

else

System.out.println(m + “ is odd");

 

6.  // This code will run until the user enters a number less than 10

System.out.print(“Enter a number greater than 10: ");

j = SafkoInput.readInt();

while (j <= 10)

{

if (j % 2 == 0)

  cout << j << "is even";

System.out.print(“Enter a number greater than 10: ");

}

 

7.  // This code will test if a number is unequal to 5

System.out.print(“Enter a number: ");

final int f = 5;

w = SafkoInput.readInt();

if (w <=> 5)

System.out.println(“This number is not 5");

 

 

8.  // This code will display a number, and it’s "half"

double m,n;

n = m/2;

System.out.print(“Please enter a number: ");

m = SafkoInput.readInt();

System.out.println(“m is: "+"m");

System.out.println(“m cut in half is: "+ n);

 

 

9.  // This is just a control structure. Why won’t it compile?

int x = 1;

while (x <= 10);

x++;

}

 

Short answers:

 

Why could the loop in question #4 be an infinite loop?

 

 

Under what conditions would you use a do/while loop rather than a while loop?

 

  Back