Ôn thi tốt nghiệp: java + uml ( in oop (using java) subject ) Question 1



tải về 0.89 Mb.
trang1/8
Chuyển đổi dữ liệu30.08.2016
Kích0.89 Mb.
#29762
  1   2   3   4   5   6   7   8

Ôn thi tốt nghiệp: JAVA + UML

( *** IN OOP (USING JAVA) SUBJECT *** )

Question 1–b

A class Car and its subclass Yugo both have a method run() which was written by the programmer as part of the class definition. If junker refers to an object of type Yugo, what will the following code do?

junker.show();


  1. The compiler will complain that run() has been defined twice

b)The show() method defined in Yugo will be called.

  1. The show() method defined in Car will be called.

  2. Overloading will be used to pick which run() is called

Question 2–e

A class design requires that a particular member variable must be accessible for direct access by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?



  1. The variable should have no special access modifier

  2. The variable should be marked private and an accessor method provided

  3. The variable should be marked private

  4. The variable should be marked public

e)The variable should be marked protected

Question 3–d

A Frame's background color is set to Color.Yellow, and a Button's background color is set to Color.Blue. Suppose the Button is added to a Panel, and the Panel is added to the Frame. What background color will be used with the Panel?



  1. Color.Green

  2. Color.Blue

  3. Color.White

  4. Color.Yellow

Question 4–b

A Java exception is an instance of __________.



  1. Exception

  2. Throwable.

  3. RuntimeException

  4. Error

  5. NumberFormatException

Question 5–d

A linked list that stores int values would be comprised(Bao gom) of a group of Nodes. We might define Node by

a) class Node

{

int next;



}

b) class Node

{

int[ ] data;



Node next;

}

c) class Node



{

Node next;

}

d) class Node



{

int data;

Node next;

}

e) class Node



{

int data;

}

Question 6–d

A statement for a constructor that invokes the no-args, default constructor in its superclass is:



  1. this.constructor();

  2. descendent classes can not call constructors of their superclass.

  3. This

  4. super();

Question 7–c

A traversal of the binary search tree in figure is given as:

12, 27, 25, 28, 20, 38, 45, 42, 35, 30

Determine one which of the following traversals has been performed





  1. Reverse PostOrder Traversal

  2. InOrder Traversal

  3. PostOrder Traversal

  4. Reverse PreOrder Traversal

  5. PreOrder Traversal

Question 8–b

A vector can be described as _______.



  1. A fixed length data structure

  2. A dynamic array

  3. A special type queue

  4. A special type of stack

Question 9–d

After execution of the code fragment below, what are the values of the variables x, a, and b?

1. int x, a = 6, b = 7;

2. x = a++ + b++;



  1. x = 13, a = 6, b = 7

  2. x = 15, a = 6, b = 7

  3. x = 15, a = 7, b = 8

  4. x = 13, a = 7, b = 8

Question 10–c

An aggregation relationship is usually represented as __________ in ___________.



  1. a data field/the aggregated class

  2. a method/the aggregated class

  3. a data field/the aggregating class

  4. a method/the aggregating class

Question 11–de

An instance of _________ are checked exceptions.



  1. Throwable.

  2. NumberFormatException

  3. Error

  4. RuntimeException

  5. Exception

Question 12–e

An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors..



  1. Error

  2. NumberFormatException

  3. Throwable.

  4. Exception

  5. RuntimeException

Question 13–e

An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.



  1. Error

  2. RuntimeException

  3. Throwable.

  4. NumberFormatException

  5. Exception

Question 14–c

Analyze the following code:

Circle c = new Circle (5);

Cylinder c = cy;



  1. The code is fine.

  2. The code has a runtime error.

  3. The code has a syntax error.

Question 15–a

Analyze the following code:

class Circle {

private double radius;

public Circle(double radius) {

radius = radius;

}

}


  1. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

  2. The program has a compilation error because it does not have a main method.

  3. The program has a compilation error because you cannot assign radius to radius.

  4. The program does not compile because Circle does not have a default constructor.

Question 16–d

Analyze the following code:

class Test {

public static void main(String[] args)

throws MyException {

System.out.println("Welcome to Java");

}

}

class MyException extends Error {



}

  1. You cannot declare an exception in the main method.

  2. The program has a compilation error.

  3. You declared an exception in the main method, but you did not throw it.

  4. You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.

Question 17–c

Analyze the following code:

class Test {

public static void main(String[] args) {

try {

Integer.parseInt(5.6); // Cause a NumberFormatException



int i = 0;

int y = 2 / i;

}

catch (Exception ex) {



System.out.println("NumberFormatException");

}

catch (RuntimeException ex) {



System.out.println("RuntimeException");

}

}



}

  1. The program displays RuntimeException.

  2. The program displays NumberFormatException.

  3. The program has a compilation error.

  4. The program displays NumberFormatException followed by RuntimeException.

Question 18–c

Analyze the following code:

class Test {

public static void main(String[] args) {

try {

int zero = 0;



int y = 2/zero;

try {


Integer.parseInt(5.6); // Causes NumberFormatExcepiton

}

catch(Exception e) {



}

}

catch(RuntimeException e) {



System.out.println(e);

}

}



}

  1. None of the other answers

  2. The program has a compilation error because Exception appears before RuntimeException.

  3. A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.

  4. A try-catch block cannot be embedded inside another try-catch block.

Question 19–c

Analyze the following code:

class WhatHappens implements Runnable {

public static void main(String[] args) {

Thread t = new Thread(this);

t.start();

}

public void run() {



System.out.println("hi");

}

}



  1. This program compiles and the word "hi' appears in the standard output, once

  2. This program compiles and the word "hi" appears continuously in the standard output until the user hits control-c to stop the program

  3. This program does not compile

  4. This program compiles but nothing appears in the standard output

Question 20–a

Analyze the following code:

Cylinder cy = new Cylinder(1, 1);

Circle c = cy;



  1. The code is fine.

  2. The code has a runtime error.

  3. The code has a syntax error.

Question 21–e

Analyze the following code.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Test extends A {

public static void main(String[] args) {

A frame = new Test();

frame.setSize(200, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

JButton jbtOK = new JButton("OK");



public Test() {

getContentPane().add(jbtOK);

jbtOK.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {



super.actionPerformed(e);

if (e.getSource() == jbtOK)

System.out.println("OK button is clicked");

}

}



class A extends JFrame implements ActionListener {

JButton jbtCancel = new JButton("Cancel");

public A() {

getContentPane().setLayout(new FlowLayout());

getContentPane().add(jbtCancel);

jbtCancel.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {



if (e.getSource() == jbtCancel)

System.out.println("Cancel button is clicked");

}

}


  1. When you click the OK button the message "OK button is clicked" is displayed.

  2. When you click the Cancel button the message "Cancel button is clicked" is displayed.

  3. The program displays Cancel button on the left of the OK button.

  4. If the super.actionPerformed(e) statement in the actionPerformed method in the Test class is omitted, no message is displayed if you click the Cancel button.

  5. All of the other answers

Question 22–d

Analyze the following code.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Test1 extends JFrame {

public Test1() {

getContentPane().add(new MyCanvas());

}

public static void main(String[] args) {



JFrame frame = new Test1();

frame.setSize(300, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class MyCanvas extends JPanel {



private String message;

public void setMessage(String message) {

this.message = message;

}

public void paintComponent(Graphics g) {



super.paintComponent(g);

g.drawString(message, 20, 20);

}

}


  1. The program has a syntax error because new Test1() is assigned to frame.

  2. The program would display Welcome to Java! if you replace new MyCanvas() by new MyCanvas("Welcome to Java!").

  3. The program runs fine and displays nothing since you have not set a string value.

  4. The program has a NullPointerException since message is null when g.drawString(message, 20, 20) is executed.

Question 23–d

Analyze the following code:

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

public class Test extends JFrame {

public Test() {

Border border = new TitledBorder("My button");

JButton jbt1 = new JButton("OK");

JButton jbt2 = new JButton("Cancel");

jbt1.setBorder(border);

jbt2.setBorder(border);

getContentPane().add(jbt1, BorderLayout.NORTH);

getContentPane().add(jbt2, BorderLayout.SOUTH);

}

public static void main(String[] args) {



JFrame frame = new Test();

frame.setSize(200, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}


  1. The program has a syntax error because you assign new TitledBorder("My button") to a variable of the Border type.

  2. The program has a runtime error because you cannot set a border on a button.

  3. Two buttons displayed, but only one button has the border.

  4. Two buttons displayed with the same border.

Question 24–d

Analyze the following code.

import java.awt.*;

import javax.swing.*;

public class Test {

public static void main(String[] args) {

Component c = new JButton("OK");

JFrame frame = new JFrame("My Frame");

frame.add(c);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}



  1. You can only add c to a container because c's type is Component.

  2. You cannot create a JFrame using new JFrame("My Frame").

  3. You cannot assign a JButton to a variable of java.awt.Component.

  4. You cannot add a Swing component directly to a JFrame.

  5. Instead, you have to add it to a JFrame's contentPane using frame.getContentPane().add(c).

Question 25–e

Analyze the following code.

import java.awt.*;

import javax.swing.*;

public class Test {

public static void main(String[] args) {

JFrame frame = new JFrame("My Frame");

frame.getContentPane().add(new MyDrawing("Welcome to Java!"));

frame.setSize(300, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setVisible(true);

}

}

class MyDrawing extends JPanel {



String message;

public MyDrawing(String message) {

this.message = message;

}

public void paintcomponent(Graphics g) {



super.paintComponent(g);

g.drawString(message, 20 ,20);

}

}


  1. The program has a runtime error because the paintcomponent should be spelled as paintComponent.

  2. The program has a syntax error because the paintcomponent should be spelled as paintComponent.

  3. The program runs fine and displays Welcome to Java!

  4. It is a runtime to invoke the setVisible(true) twice.

  5. The program runs, but it does not display the message.

Question 26–b

Analyze the following code.

import java.awt.*;

import javax.swing.*;

public class Test {

public static void main(String[] args) {

JFrame frame = new JFrame("My Frame");

frame.getContentPane().add(new JButton("OK"));

frame.getContentPane().add(new JButton("Cancel"));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(200, 200);

frame.setVisible(true);

}

}


  1. Both button OK and button Cancel are displayed and button OK is displayed on the left side of button OK.

  2. Only button Cancel is displayed.

  3. Both button OK and button Cancel are displayed and button OK is displayed on the right side of button OK.

  4. Only button OK is displayed.

Question 27–b

Analyze the following code:

import javax.swing.*;

import java.awt.*;

public class Test extends JFrame {

public Test() {

getContentPane().setLayout(new FlowLayout());

getContentPane().add(new JButton("Java"));

getContentPane().add(new JButton("Java"));

getContentPane().add(new JButton("Java"));

getContentPane().add(new JButton("Java"));

}

public static void main(String[] args) {



// Create a frame and set its properties

JFrame frame = new Test();

frame.setSize(200, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}



  1. One button is displayed with the text "Java".

  2. Four buttons are displayed with the same text "Java".

  3. Three buttons are displayed with the same text "Java".

  4. Two buttons are displayed with the same text "Java".

Question 28–b

Analyze the following code:

import javax.swing.*;

public class Test extends JFrame {

private JButton jbtOK = new JButton("OK");

public static void main(String[] args) {

// Create a frame and set its properties

JFrame frame = new Test();

frame.setTitle("Logic Error");

frame.setSize(200, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

public Test() {



jbtOK.setToolTipText("This is a button");

getContentPane().add(new JButton("OK"));

}

}


  1. The tool tip text is displayed when you move the mouse on the button.

  2. The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK).

  3. The tool tip text will be displayed if you swap the two lines in the Test constructor.

  4. The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK = new JButton("OK")).

Question 29–bc

Analyze the following code:

import java.util.StringTokenizer;

public class A extends StringTokenizer {

}


  1. The program would compile fine if you add the following constructor into A:

A(String s) { }

  1. The program has a compilation error because the default constructor of A invokes the default constructor of StringTokenizer, but StringTokenizer does not have a default constructor.

  2. The program would compile fine if you add the following constructor into A:

A(String s) { super(s); }

  1. The program has a compilation error because A does not have a default constructor.

Question 30–c

Analyze the following code.

Number[] numberArray = new Integer[2];

numberArray[0] = new Double(1.5);



  1. Since each element of numbeArray is of the Number type, you cannot assign an Integer object to it.

  2. You cannot use Number as a data type since it is an abstract class.

  3. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.

  4. Since each element of numbeArray is of the Number type, you cannot assign a Double object to it.

Question 31–a

Analyze the following code.

public class Test {

int x;


public Test(String t) {

System.out.println("Test");

}

public static void main(String[] args) {



Test test = new Test();

System.out.println(test.x);

}

}


  1. The program has a syntax error because Test does not have a default constructor.

  2. The program has a syntax error because x has not been initialized.

  3. The program has a syntax error because you cannot create an object from the class that defines the object.

  4. The program has a syntax error because System.out.println method cannot be invoked from the constructor.

Question 32–c

Analyze the following code.

public class Test {

int x;


public Test(String t) {

System.out.println("Test");

}

public static void main(String[] args) {



Test test = null;

System.out.println(test.x);

}

}


  1. The program has a syntax error because Test does not have a default constructor.

  2. The program has a syntax error because you cannot create an object from the class that defines the object.

  3. The program has a runtime NullPointerException because test is null while executing test.x.

  4. The program has a syntax error because test is not initialized.

  5. The program has a syntax error because x has not been initialized.

Question 33–d

Analyze the following code:

public class Test {

int x;


{ x++; }

}


  1. The program cannot be compiled, because the statement x++ must be placed inside a method or a constructor.

  2. When you construct an instance of Test, the value of x becomes 0;

  3. You cannot construct an instance of Test, because it does not have a constructor.

  4. When you construct an instance of Test, the value of x becomes 1;

Question 34–a

Analyze the following code:

public class Test {

int x;


static { x++; }

}


  1. The program cannot be compiled, because x is non-static, but is used in a static initialization block.

  2. The program cannot be compiled, because the statement x++ must be placed inside a method or a constructor.

  3. When you construct an instance of Test, the value of x becomes 0;

  4. When you construct an instance of Test, the value of x becomes 1;

Question 35–a

Analyze the following code:

public class Test {

public static void main(String[] args) {

double radius;

final double PI= 3.15169;

double area = radius * radius * PI;

System.out.println("Area is " + area);

}

}


  1. The program has syntax errors because the variable radius is not initialized.

  2. The program compiles and runs fine.

  3. The program has a syntax error because a constant PI is defined inside a method.

  4. The program has no syntax errors but will get a runtime error because radius is not initialized.

Question 36–bde

Analyze the following code:

public class Test {

public static void main(String[] args) {

HastSet set1 = new HashSet();

set1.add("red");

Set set2 = set1.clone();

}

}



  1. The program will be fine if set1.clone() is replaced by (Set)set1.clone()

  2. The program will be fine if set1.clone() is replaced by (Set)(set1.clone())

  3. Line 5 is wrong because a HashSet object cannot be cloned.

  4. The program will be fine if set1.clone() is replaced by (HashSet)(set1.clone())

  5. Line 5 has a syntax error because set1.clone() returns an Object.

  6. You have to cast it to Set in order to compile it.

Question 37–b

Analyze the following code:

public class Test {

public static void main(String args[]) {

NClass nc = new NClass();

nc.t = nc.t++;

}

}

class NClass {



int t;

private NClass() {

}

}


  1. The program compiles and runs fine.

  2. The program has a compilation error because the NClass class has a private constructor.

  3. The program compiles, but has a runtime error because t has no initial value.

  4. The program does not compile because the parameter list of the main method is wrong.


Каталог: 2011
2011 -> HƯỚng dẫn viết tiểu luậN, kiểm tra tính đIỂm quá trình môn luật môi trưỜNG
2011 -> Dat viet recovery cứu dữ liệu-hdd services-laptop Nơi duy nhất cứu dữ liệu trên các ổ cứng Server tại Việt Nam ĐC: 1a nguyễn Lâm F3, Q. Bình Thạnh, Tphcm
2011 -> Ubnd tỉnh thừa thiên huế SỞ giáo dục và ĐÀo tạO
2011 -> SỞ TƯ pháp số: 2692 /stp-bttp v/v một số nội dung liên quan đến việc chuyển giao CỘng hòa xã HỘi chủ nghĩa việt nam
2011 -> QUỐc hội nghị quyết số: 24/2008/QH12 CỘng hoà XÃ HỘi chủ nghĩa việt nam
2011 -> NĐ-cp cộng hòa xã HỘi chủ nghĩa việt nam độc lập – Tự do – Hạnh phúc
2011 -> BỘ NỘi vụ CỘng hoà XÃ HỘi chủ nghĩa việt nam
2011 -> Nghị quyết số 49-nq/tw ngàY 02 tháng 6 NĂM 2005 CỦa bộ chính trị VỀ chiến lưỢc cải cách tư pháP ĐẾn năM 2020
2011 -> Ủy ban nhân dân tỉnh bà RỊa vũng tàU
2011 -> Ủy ban nhân dân cộng hòa xã HỘi chủ nghĩa việt nam thành phố HỒ chí minh độc lập Tự do Hạnh phúc

tải về 0.89 Mb.

Chia sẻ với bạn bè của bạn:
  1   2   3   4   5   6   7   8




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương