COP 2551 Exam 1 Name _____________________

advertisement
COP 2551 Exam 1
Name _____________________
Each question worth 10 points. This is a closed book exam.
1.What is printed by the following program:
import java.io.*;
class expressions
{
public static void main(String[] args) throws IOException
{
int x = 0;
double z = 4.0;
x = 6/3;
System.out.println("x = " + x);
x= x/2;
System.out.println("x = " + x);
x = (int) z + 1;
z = --x;
// Hint: when does the -- actually take place
System.out.println("z = " + z);
z = x % 2;
System.out.println("z = " + z);
z = (x+z)/z + 1;
System.out.println("z = " + z);
}
}
// end main()
// end expressions
2. Circle all syntax errors in the following program:
import java.io.*;
public class prog1
{
public static void main (String[] args)
{
int x;
int tax;
z = 0.0;
tax = 6.0;
System.out.print ("Enter dollar amount: ")
x = getFloat();
tax = z;
String y;
7 = 7;
if (z = y) x == 1;
}
3. What is printed by the following program when we execute the command “java prog1 10”
import java.io.*;
public class prog1
{
public static void main (String[] args) throws IOException
{
double x = 3.6;
int num;
num = Integer.parseInt(args[0]);
if (num != 10) {
num = (int) x/2;
}
else
if (x == 2.0) num = 2;
else num = (num * (int) x);
System.out.println(“Final Amount = “ + num);
}
}
// end class prog1
4. Write a Java program that does the following:
Inputs two integers (you may assume that you have a command getInt() to do this).
Print the smallest integer greater than the larger of the two inputs.
5. Write a Java program that defines a complex number as having two data items: int_part an
imaginary _part (each are integers). You must write all necessary methods to execute the
following “main” including a method to add two complex numbers and return a number that is
the sum.
public static void main (String[] args) throws IOException
{
number x, y, z;
x = new number(3, 14);
y = new number(1, 99);
z = x.add(y);
z.printnumber();
}
This should print “4 + 113i “ (“i” denotes the imaginary_part).
Download