Fall 99 ISQS 6337 Exam 1

Total Page:16

File Type:pdf, Size:1020Kb

Fall 99 ISQS 6337 Exam 1

Spring 2004 ISQS 6337 Exam 1

1. Explain the difference between: (5)

BankAccount b; and BankAccount b = new BankAccount(5000);

the first creates a reference to a BankAccount class but does not instantiate an object the second creates the reference and instantiates the BankAccount object

2. Write a method that takes in an array of integers (as int's) and returns the smallest in the group. (15)

see question 5

3. What methods in an Applet, or in a JApplet, are called ONLY when the program first starts? (5)

The constructor & init()

4. What is printed given the following code? (5) int x = 15; System.out.println((x%2 == 0) ? "Even" : "Odd");

Odd 5. Write an application that reads in five integers and determines and prints the largest and the smallest in the group. (20)

//Something like import javax.swing.JOptionPane; public class Ex1_Q5 {

//Main method public static void main(String[] args) { int[] iaNumbers = new int[5]; int iLow, iHigh;

for (int i = 0; i<5; i++) { iaNumbers[i] = Integer.parseInt( JOptionPane.showInputDialog("Enter an integer please")); } iLow = getLowest(iaNumbers); iHigh = getHighest(iaNumbers);

System.out.println("Low = " + iLow + "\nHigh = " +iHigh); } // end of main public static int getLowest(int[] a) { int iCurrent = a[0];

for (int i = 1; i < a.length; i++) if (a[i] < iCurrent) iCurrent = a[i];

return iCurrent; } // end of getLowest public static int getHighest(int[] a) { int iCurrent = a[0];

for (int i = 1; i < a.length; i++) if (a[i] > iCurrent) iCurrent = a[i];

return iCurrent; } // end of getHighest

} // end of class

6. Write the code to create a class called square. The class has: (20) a. only one attribute, width, that is an int b. a default constructor that creates a square with a width of 0 c. a one argument constructor that takes width as an argument d. a single method that returns the area of the square public class square { private int _width;

public square() { _width = 0; }

public square (int w) { _width = w; }

public int getWidth() { return _width; }

public void setWidth(int w) { _width = w; }

public int getArea() { return _width * _width; } }

7. Write the code to instantiate a square with a width of 150 and to print out it's area. (10)

square s = new square(150); System.out.println(s.getArea());

8. What does the following program sequence do? (10) for (int i = 1; i <= 2; i++) { for(int j = 1; j <= 3; j++) { for(int k=1;k%10 > 0;k++) System.out.print('*'); System.out.println(); } System.out.println(); } ********* ********* *********

********* ********* *********

9. What does the following code do? (10) int i = 5; while (i >= 0) { switch (i) { case 5: System.out.print("F "); case 4: System.out.print("E "); case 3: System.out.print("D "); case 2: System.out.print("C "); case 1: System.out.print("B "); default: System.out.print("A "); } System.out.print(); i--; }

F E D C B A E D C B A D C B A C B A B A A

Recommended publications