Reference no: EM13494529
Question 1:
(a) Show the printout of the following code:
public class Test {
public static void main(String[] args) {
int[] a = {1, 2};
swap(a[0], a[1]);
System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); }
public static void swap(int n1, int n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
}
(b) Create a panel with BorderLayout. Add a button labeled "OK" to the south side of the panel.
(c) Given the following program, show the values of the array in the following figure:
public class Test {
public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i;
}
values[0] = values[1] + values[4];
}
}
(d) Suppose A is an abstract class, what is wrong in the following code?
A[] list = new A[10];
list[0] = new A();
QUESTION 2
(a) Write a program that passes an unspecified number of integers as one command-line argument and displays their total. For example, if you run the program using the following argument,
java Sum "1 2 3"
The output is
Sum is 6.
(b) Write a program that reads words from a text file and displays all the nonduplicate words in ascending order. The text file is passed as a command-line argument.
(c) Write a method to find the max in an array of double values. The method signature is as follows:
public static double max(double[] a)
QUESTION 3
The following diagram shows the abstract class GeometricObject.
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
so, the get method name is isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Return a string representation of this object */
public String toString() {
return "\ncolor: " + color + " and filled: " + filled;
}
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
(a)
Design a class named Triangle that extends GeometricObject. The class contains three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. Implement the getPerimeter() that returns the perimeter of this triangle
(b) Re-design the class Triangle that also implements Comparable. The triangles are compared in terms of their perimeters.
(c)
Design a new class named Polygon that extends GeometricObject and implements Comparable. The number of sides in polygons varies, you cannot use a certain number of data fields for sides of a polygon. For example a triangle is seen as a polygon with three sides and a rectangle is a polygon of four sides. Please use a List of Double values to denote the length of each side of a polygon. Implement the getPerimeter() that returns the perimeter of this triangle
QUESTION 4
Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars. The layout should be designed following the draft layout shown in the second diagram.
Question 5:
Answer the following questions by choosing the single most correct alternative from those given.
1. The method parses a string s to a double value.
a. double.parseDouble(s);
b. Double.parsedouble(s);
c. double.parseDouble(s);
d. Double.parseDouble(s);
2. What is the printout of the following code:
double x = 10.1;
int y = (int)x;
System.out.printIn("x is " + x + " and y is " + y):
a. x is10 and y is 10
b. x is 10.0 and y is 10.0
c. x is 11 and y is 11
d. x is 10.1 and y is 10
3. Which of the following is not a valid boolean expression.
a. (1 < x < 100);
b. (x = 1)11(x I= 1)
c. (x =< 5) & (x>=5)
d. The above are all wrong
4. Given the following method:
static void nPrint(String message, Int n) ( while (n > 0) {
System.out.print(message);
n-;
}
}
What is the printout of the call nPrint("a". 4)?
a. aaaaa
b. aaaa
c. aaa
d. invalid call
4. An array reference variable can be used in which of the following ways?
a. As a local variable
b. As a parameter of a method
c. Asa return value of a method
d. All of the above
5. Which of the following are valid array declarations
a. char(' charArray = new char(261:
b. Intl] words = new words[10];
c. chart] charArray = Computer Science";
d. double[3] nums = (3.5, 35.1, 32.0};
6. Suppose s1 and s2 are two strings. which of the following statements or expressions are Incorrect?
a. String s3 = 51 - s2;
b. int i = s1.compareTo(s2);
c. int j = s2.1ength():
d. char c = s1.charAt(stlength() - 1):
7. How can you Initialize a string with "123"?
a. String] string = {'l', '2', '3'};
b. String s = "123":
c. String s = new String("123");
d. both (a) and (c) are both fine, but (b) is better
8. Which method can be used to create an output object for file temp.td?
a. new Formatter('lemp.brt")
b. new Formatter(temp.bct)
c. new Formatter(new File("tempixt"))
new Formatter(File("tematd"))
9. When you implement a method that is defined in a superclass. you
a. Overload
b. Override
c. Copy
d. Call
10. The class is inherited by every Java class.
a. Class
b. Object
c. Number
d. Comparable
11. What method do you use to set the location of a frame on the screen?
a. setPosition
b. selLocation
c. setsize
d. setDimention the original method
12. What is the default layout manager for the content pane of a JFrame ?
a. FlowLayout
b. BorderLayout
c. GirdLayout cl. No default layout manager
13. What is the default layout manager for a JPanel?
a. FlowLayout
b. Borderlayout
c. GirdLayout
d. No default layout manager
14. draws a drcle whose center position is (400, 400) and whose radius is 50.
a. g.drawCircle(350, 350, 100, 100)
b. g.drawOval(350. 350, 100, 100)
c. g.drawCircle(400. 400. 50. 50)
d. g.drawOvale(400. 400. 50. 50)
15. What method do you use to set the text to the right of the icon in an instance jbt of JButton?
a. jbtsetHorizontalArignment(SwingContants.RIGHT)
b. bt.setVerticalAlignment(SwingContants.RIGHT)
c. btsetHorizontalTextPosItion(SwingContants.RIGHT)
d. btsetVerlicalTextPosition(SwingContants.RIGHT)
16. What method do you use to disable editing on an Instance jtf of JTextField?
a. jtt.setEditing(false)
b. jtt.setEditable(false)
c. ittenableEditing(false)
d. jltsetEcIlt (false)
17. Can a JComboBox instance generate the following events?
a. MouseEvent
b. ActionEvent
c. ItemEvent
d. All above
18. The method that executes immediately after the init() method in an applet is
a. destroy()
b. start()
c. stop()
d. run()
19. Which data structure is appropriate to store customers in a clinic for taking flu shots?
a. Stack
b. Queue
c. Priority Queue
d. Array List
20. Suppose List<String> list = new ArrayList<String>. Which of the following operations are correct?
a. list.add("Red");
b. list.add(new lnteger(100)):
c. list.add(new java.uULDate());
d. list.add(new ArrayList()):
21. To declare a class named A with two generic types. use
a. public class A<E>
b. public class A<E, F>
c. public class A(E) 1
d. public class A(E, F) }
22. Which of the data type below does not allow duplicates?
a. Set
b. List
c. Stack
d. LinkedList
23. Analyze the following code
import java.util.*: public class Test {
public static void main(String0 args) throws Exception (
Set set = new TreeSet();
setadd("Red"); setadd("Green"): set.add("Blue");
System.out.println(set.first());
}
}
a. The program displays Red
b. The program displays Blue
c. The program displays Green
d. The program may display Red, Blue, or Green.
24. which method do you use to test tt an element Is Ina set or list named tc?
a. (element instanceof [SOU (element instanceof Set)
b. xin(element)
c. x.contains(element) 0. x inctude(element)
25. Which method do you use to mid the number of elements In a set or list named r?
a. x.length()
b. x.count()
c. x.number()
0. x Size()
26. WhIch method do you use to remove an element from a set or list named x? a. x.delete(eleMent)
D. xremove(element)
c. x deletes(element)
d. x remOves(element)
27. wntr_n of the following is correct to sort the elements In a list Ist? a. 1St sort()
D. C011eCtiOnS.SOrt(ISt)
c. Arrays.sort(Ist)
d. new linkedList(new StrIngrred". 'green". "blue"))
29. Which method on a condition should you Invoke to causes the current thread to wait until the condition Is signalled?
a. ConditiOn.vraiteda
b. COnditiOn.awarta
c. comlnion.waitinga
0. COnditiOn.waita
30 Suppose that your program accesses MySOL or Oracle database. Which c4 to follovang staterneMs are true?
a. d the driver for MySOL and Oracle are not in the classpath, the program M have a runbme error. indicating that the driver Naas cannot be loaded
b. the database is not available. the program will have a runtime error. when attempting to create a Connection object.
C. If the database is not available. the program will have a syntax error.
d if the driver for MySOL and Oracle are not in the dasspar, the program ws have a syntax error
30. Analyze the following code.
ResubSel resullSet statement execuleOuery
(select arStName. ml, lastName from Student where laniMme" + " = 'smith");
System.out println(resullSet gelStrIng(1)).
Minch Of to toarmIng are right? (multiple Answers)
a if the SOL SELECT statemeM returns no result resubSet is null
b. resultSel getStnng(1) returns the erstName field in the mesa set
c mune! getStmg(1) returns the nv field in the result set
d The program MI have a runtime error. because the cursor in resultSet does not point to a row. You must use resubSet next() to move the cursor to the ersi row in the result set Subsequently. restaiSet mead() moves the cursor to the neat row in the result set
31 in a relational data model. provides the means for accessirg and manrthating data
a SOL
a Executor
C Structure
m Integrity
32 whim of the folloMng statements are true? (Muni* Afts*Cfs)
a You may load multiple ADC drivers xi a program
0. You may create multiple connections to a database.
C You Can send buena and update statements through a Statement object
m YOU may <reale multiple statements from one connection
33 TO execute a SELECT statement 'SOW • from Address' on a Statement oiled stmt. use a stmt execulerselect • from Andress"),
b. at m duery("Select * from Andreas").
C SIN execuleOumy(seled * from Address").
d situ execuleUmlaterseled * frC01 Addf CSC).
14 lAtuch of the lodging statements is not true?
a. A panel can be placed inside a panel.
b. A frame can be placed inside a frame.
c. A source object and a listener °bled can be the same.
d. A source object can register many listeners.
35. Mat should you use to positon a button with.. an application frame so that the size of the button is NOT affedeci by the frame size? a. a FlOvelayOut
b a GndlayOut
c. the center area of a BorderLayout
0 the East or Weal area of a BorderlayOut
e the North or South area of a BorderLayotil
36 F at wo the code in Comparable c = new Data();
a. <String>
b <1>
C <Date>
d. <E>
37. Analyze the following code
import java.utr;
public class Test (
public static void main (String II args) throws Exception
TreeSet set = new TreeSet();
set set.add("Yelow"); set.add("Green"); set.add("Blue"); SortedSet temp = set.headSet("Purple");
System.oul.println(temptirst());
a. The program displays Blue
b. The program displays Red
c. The program displays Green
d. The program displays Yellow
e. The program displays Purple
38. Which of the following is correct to create a list from an array?
a. new List(("red", "green', "blue"))
b. new List(new Stringrred", "green", "blue"))
c. Arrays.asList(new Stringrred", "green", "blue"))
d. new ArrayList(new Stringu("red", "green", "blue"))
e. new LinkedList(new Stringrred", "green", "blue"))
39. Much layout manager would you use to place three GUI components in a frame so that all components have the same sae?
a. GndLayout
b. F lowLayoul
C. 8orderLayout
d. CardLayout