CISC 213- Practice Test 1 Part A- The following questions are either multiple choice or short answer. If the question is multiple choice, select the best answer. 1.

Which of the following is true about every Java application: a) It must must have at least one import statement. b) It must have at least one statement involving System.out.println(). c) It must have a class which extends the class Applet or JApplet.. d) It must have a class containing the main() function. e) None of these

2.

Say we are using JDK and we want to compile a file called Test.java. What command do we type at the command prompt?

3.

Say we have compiled an application called Test.java using JDK. What command do we type at the command prompt to execute this program?

4.

Say we have compiled an applet called Test.java. What must be created to execute this program using JDK?

5.

Which of the following variables is not a reference variable: a) Font f b) int i c) Integer i d) Graphics g e) None of these

6.

If we have defined a class called Rectangle then what does the following declaration do: Rectangle r ?

7.

If we have defined a reference variable r for a class Rectangle, what does r = new Rectangle( ) do?

8.

In java, swing is a a) class b) container

c) package

d) file

e) None of these

9.

If we need to declare a decimal point number which takes up the least amount of space, then this variable must be of type a) long b) float c) byte d) double e) int

10.

Explain why a Java applet must have more restrictions than a Java application.

11.

If A is a class with one public function f1 and B is a subclass of A with one public function f2, then which of the following is true assuming x is an instance of B: a) x has only one function b) x has at least two functions

c) x must have exacatly 2 functions e) None of these

d) x may have 1 or 2 functions, no more

12.

Say we define the class called A as follows: class A { void z(int a) { System.out.println("Number = " + a); } } Which of the following is legitimate? Assume code is contained in a class that is unreleated to A. a) A.z( ) b) z( ) c) A.z(5.2) d) z(5) e) None of these

13.

Because a character in Java is 16 bits, Java has the capability of using a) only the ASCII characters b) all of the Unicode characters c) only the ASCII characters and the extended ASCII characters d) the Unicode characters, but not the ASCII characters e) None of these

14.

Consider the following code: int i = 7; do { System.out.println("Test"); i++; while (i < 8); How many times will Test be printed out? a) 1 time b) 2 times c) 3 times d) 10 times e) It won't be printed at all

15.

When we execute an applet which of the following is true? a) init() is executed first and destroy() is executed last b) init() is executed first and stop() is executed last c) start() is executed first and destroy() is executed last d) start() is executed first and stop() is executed last e) None of these

16.

System.out.println(5 > 9) will output a) 0 b) FALSE c) False d) false

17.

18.

e) None of these

Which of the following declarations is incorrect? a) int[ ] x b) int x[ ] c) int x[ ] = new int[12] e) They are all correct Consider the code:

d) int x[ ] = {1, 2, 4}

{ int x = 1; { int y = 2; } //Comment By the time we reach the comment, which of the following variables are out of scope? a) Only x b) x and y c) Only y d) It depends on the what follows e) None of these 19.

Say we define the following classes: class A { double x; } class B extends A { double y; } Which of the following is illegal assuming that a is an instance of A and b is an instance of B? a) a.x = 5 b) a.y = 5 c) b.x = 5 d) b.y = 5 e) They are all legal

20.

Consider the following code and determine which statements will give compile errors: class Primitives { public static void main(String[] args) { byte b = 5; int i = 6; float f = 7; double d = 8; b= i; i = b; f = i; f = d; d = f; d = b; }

} 21.

Explain what is incorrect about the statement if (x = 5) System.out.println(x) .

22.

If we make the declaration String a[] = {"one", "two", "three"} then explain what a[0], a[1], and a[2] represent.

23.

Assume we make the declarations double[][] abc = new double[3][]; abc[0] = new double[5]; abc[1] = new double[3]; abc[2] = new double[7]; What is it that we're doing? What are abc[0], abc[1], and abc[2]? What is wrong with abc[1][3]? If we did do this, when will the error occur?

Part B- What is the output from the following programs? 1. class A { static int a = 5; } class p4 { public static void main(String[] args) { String s = "Test"; String t = "Case"; A x, y; x = new A(); y = new A(); x.a = 10; y.a = 120; t += s; System.out.println(t + (++x.a)); System.out.println(s + (x.a++)); System.out.println(x.a); } } 2. class pt1 { public static void main(String[] args) { String a = "Hortense"; String b = "Hortense"; String c = a + b; System.out.println(a+b+c); System.out.println(a==b); System.out.println(a==a);

String d = new String("Hortense"); System.out.println(d); System.out.println(d + b); System.out.println(d==b); } } 3. import java.awt.*; import java.applet.*; public class pt2 extends Applet { public void paint (Graphics g) { int[] x = {1, 2, 5, 3}; g.drawString("Hortense", 10, 10); for (int i = 0; i < 4; i++) { g.drawString("Num= " + x[i] + 8, 10, 20*i+40); } } } 4. import java.awt.*; import java.applet.*; public class pt3 extends Applet { String msg; int i = 0; public void init() { setBackground(Color.red); setForeground(Color.blue); msg = "Bob, "; i += 4; } public void start() { msg += "Carol, "; i += 4; } public void paint(Graphics g) { msg += "Ted, and Alice";

i += 4; g.drawString(msg, 10, 30); g.drawString("i = " + i, 10, 50); } } 5.

class Abc { static int x; int y; } class StaticEx { public static void main(String[] args) { Abc z1 = new Abc(); Abc z2 = new Abc(); z1.x = 5; z1.y = 6; z2.x = 7; z2.y = 8; System.out.println(z1.x + ", " + z1.y); } }

6.

import javax.swing.*; class SwitchEx { public static void main(String[] args) { int i = 0, choice = 1; switch (choice) { case 1: i++; case 2: i++; case 3: i++; default: i++; } JOptionPane.showMessageDialog(null, "The value of i is " + i); System.exit(0); } }

7.

// Assume there are no param tags in our htm file import java.awt.*;

import java.applet.*; public class Applet1 extends Applet { String x = "100"; String y = "200"; String s = "Zeke"; public void init() { s = getParameter("abc"); if (s == null) s = "Now is the time"; else s = "The quick brown fox"; } public void paint(Graphics g) { String x = "5"; g.drawString(x, 20, 40); g.drawString(y, 20, 60); g.drawString(s, 20, 80); int i; for (i = 0; i < 3; i++); //Be careful g.drawString("Hortense", 20, 100+20*i); } } Part C- Each of the following programs has one error. What is this error? For part 3, there is one error that will stop the program from executing and it contains a statment that may cause problems in a larger program. 1) import java.awt.*; public class Applet1 extends java.applet.Applet { public void init() { String s = "Test"; } public void paint(Graphics g) { g.drawString(s, 10, 10); } }

2) import javax.*; class PartC { public static void main(String[] args) { double i = 0; JOptionPane.showMessageDialog(null, "The value of i is " + i); System.exit(0); } } 3) import java.awt.*; import javax.swing.*; public class PartC3 extends JApplet { int x = 10; JTextField t; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); JTextField t = new JTextField(x); c.add(t); t.setText(4); } }