15
Page 1 of 15 Instruction to students: 1. Language allowed to answer is the English language. 2. You should attempt 40 out of the 50 MCQ questions in Section I. 3. You should attempt all of Section II (Section II comprises questions 2, 3, and 4). 4. The exam paper is 14 pages long, and is in 2 sections. 5. The approximate allocation of marks is shown in brackets by the questions. 6. Section I contains multiple choice questions. Answer for the multiple choice questions should be written in the next table move it to the answer sheet. Section I : Key answer for the multiple choice questions Questions 1 2 3 4 5 6 7 8 9 10 Answer Key Questions 11 12 13 14 15 16 17 18 19 20 Answer Key Questions 21 22 23 24 25 26 27 28 29 30 Answer Key Questions 31 32 33 34 35 36 37 38 39 40 Answer Key Questions 41 42 43 44 45 46 47 48 49 50 Answer Key Benha University Faculty of Computers & Informatics 2 nd Term (2012) Final Exam Class: 2 nd Year Students Subject: Object Oriented Programming Date: 20/5/2012 Time: 3 hours Examiner: Dr. Essam Halim

1. English language 2. 40 Section I 3. - Bu and Informatics/Computer... · Language allowed to answer is the English language. 2. You should attempt 40 out of the 50 MCQ questions

  • Upload
    lamdien

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1 of 15

Instruction to students:

1. Language allowed to answer is the English language.

2. You should attempt 40 out of the 50 MCQ questions in Section I.

3. You should attempt all of Section II (Section II comprises questions 2, 3, and 4).

4. The exam paper is 14 pages long, and is in 2 sections.

5. The approximate allocation of marks is shown in brackets by the questions.

6. Section I contains multiple choice questions. Answer for the multiple choice questions

should be written in the next table move it to the answer sheet.

Section I : Key answer for the multiple choice questions

Questions 1 2 3 4 5 6 7 8 9 10

Answer Key

Questions 11 12 13 14 15 16 17 18 19 20

Answer Key

Questions 21 22 23 24 25 26 27 28 29 30

Answer Key

Questions 31 32 33 34 35 36 37 38 39 40

Answer Key

Questions 41 42 43 44 45 46 47 48 49 50

Answer Key

Benha University

Faculty of Computers & Informatics

2nd Term (2012) Final Exam Class: 2nd Year Students Subject: Object Oriented Programming

Date: 20/5/2012 Time: 3 hours Examiner: Dr. Essam Halim

Page 2 of 15

Answer the following questions:

Section I [Total 60]

Q(1): Multiple choice questions

Answer only 40 of the following 50 multiple choice questions, by selecting the correct answer in each.

Place the answer on the special MCQ form. Each question [1.5 Mark]

1 If a number is too large to be stored in a variable of the float type, it _____________.

A. causes overflow

B. causes underflow

C. causes no error

D. cannot happen in Java

2 What is i printed in the following code?

public class Test {

public static void main(String[] args) {

int j = 0;

int i = j++ + j * 5;

System.out.println("What is i? " + i);

} }

A. 0

B. 1

C. 5

D. 6

3 Analyze the following code.

import javax.swing.*;

public class ShowErrors {

public static void main(String[] args) {

int i;

int j;

String s = JOptionPane.showInputDialog(null,

"Enter an integer", "Input",

JOptionPane.QUESTION_MESSAGE);

j = Integer.parseInt(s);

i = (i + 4);

} }

A. The program cannot compile because j is not initialized.

B. The program cannot compile because i does not have an initial value when it is used in i = i + 4;

C. The program compiles but has a runtime error because i does not have an initial value when it is

used in i = i + 4;

D. The program compiles and runs fine.

Page 3 of 15

4 Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10).

A. 9

B. 10

C. 11

5 What is y after the following statement is executed?

x = 0;

y = (x > 0) ? 10 : -10;

A. -10

B. 0

C. 10

D. 20

E. Illegal expression

6 How many times will the following code print "Welcome to Java"?

int count = 0;

do {

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

count++;

} while (count < 10);

A. 8

B. 9

C. 10

D. 11

E. 0

7 Is the following loop correct?

for (; ; );

A. Yes

B. No

8 How many times is the println statement executed?

for (int i = 0; i < 10; i++)

for (int j = 0; j < i; j++)

System.out.println(i * j)

A. 100

B. 20

C. 10

D. 45

Page 4 of 15

9 What is sum after the following loop terminates?

int sum = 0;

int item = 0;

do {

item++;

sum += item;

if (sum >= 4) continue;

}

while (item < 5);

A. 15

B. 16

C. 17

D. 18

10 Will the following program terminate?

int balance = 10;

while (true) {

if (balance < 9) continue;

balance = balance - 9;

}

A. Yes

B. No

11 The signature of a method consists of ____________.

A. method name

B. method name and parameter list

C. return type, method name, and parameter list

D. parameter list

12 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

Page 5 of 15

13 The client can use a method without knowing how it is implemented. The details of the implementation are

encapsulated in the method and hidden from the client who invokes the method. This is known as __________.

A. information hiding

B. encapsulation

C. method hiding

D. simplifying method

14 __________ is to implement one method in the structure chart at a time from the top to the bottom.

A. Bottom-up approach

B. Top-down approach

C. Bottom-up and top-down approach

D. Stepwise refinement

15 Which of the following statements is valid?

A. int i = new int(30);

B. double d[] = new double[30];

C. int[] i = {3, 4, 3, 2};

D. char[] c = new char();

E. char[] c = new char[4]{'a', 'b', 'c', 'd'};

16 Analyze the following code:

public class Test {

public static void main(String[] args) {

final int[] x = {1, 2, 3, 4};

int[] y = x;

x = new int[2];

for (int i = 0; i < y.length; i++)

System.out.print(y[i] + " ");

}

}

A. The program displays 1 2 3 4

B. The program displays 0 0

C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.

D. The elements in the array x cannot be changed, because x is final.

17 When you return an array from a method, the method returns __________.

A. a copy of the array

B. a copy of the first element

C. the reference of the array

D. the length of the array

Page 6 of 15

18 Which of the following declarations are correct?

A. public static void print(String strings, double numbers)

B. public static void print(double numbers, String name)

C. public static double print(double d1, double d2)

D. public static void print(double numbers)

E. public static void print(int n, double numbers)

19 What is the printout of the following program?

public class Test {

public static void main(String[] args) {

int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

for (int row = 0; row < values.length; row++) {

System.out.print(m(values[row]) + " ");

}

}

public static int m(int[] list) {

int v = list[0];

for (int i = 1; i < list.length; i++)

if (v < list[i])

v = list[i];

return v;

}

}

A. 3 33

B. 1 1

C. 5 6

D. 5 33

E. 33 5

20 Which of the following statements are true?

A. Multiple constructors can be defined in a class.

B. Constructors do not have a return type, not even void.

C. Constructors must have the same name as the class itself.

D. Constructors are invoked using the new operator when an object is created.

21 Variables that are shared by every instances of a class are __________.

A. public variables

B. private variables

C. instance variables

D. class variables

Page 7 of 15

22 You should add the static keyword in the place of ? in Line ________ in the following code:

1 public class Test {

2 private int age;

3

4 public ? int square(int n) {

5 return n * n;

6 }

7

8 public ? int getAge() {

9 }

10}

A. in line 4

B. in line 8

C. in both line 4 and line 8

D. none

23 Analyze the following code.

public class Test {

public static void main(String[] args) {

int n = 2;

xMethod(n);

System.out.println("n is " + n);

}

void xMethod(int n) {

n++;

} }

A. The code has a compile error because xMethod does not return a value.

B. The code has a compile error because xMethod is not declared static.

C. The code prints n is 1.

D. The code prints n is 2.

E. The code prints n is 3.

24 Analyze the following code:

public class Test {

private int t;

public static void main(String[] args) {

int x;

System.out.println(t);

} }

A. The variable t is not initialized and therefore causes errors.

B. The variable t is private and therefore cannot be accessed in the main method.

C. t is non-static and it cannot be referenced in a static context in the main method.

D. The variable x is not initialized and therefore causes errors.

E. The program compiles and runs fine.

Page 8 of 15

25 What is the output of the following program?

import java.util.Date;

public class Test {

public static void main(String[] args) {

Date date = new Date(1234567);

m1(date);

System.out.print(date.getTime() + " ");

m2(date);

System.out.println(date.getTime());

}

public static void m1(Date date) {

date = new Date(7654321); }

public static void m2(Date date) {

date.setTime(7654321); }

}

A. 1234567 1234567

B. 1234567 7654321

C. 7654321 1234567

D. 7654321 7654321

26 What is the output of the following code?

public class Test {

public static void main(String[] args) {

String s1 = "Welcome to Java!";

String s2 = "Welcome to Java!";

if (s1 == s2)

System.out.println("s1 and s2 reference to the same String object");

else

System.out.println("s1 and s2 reference to different String objects");

} }

A. s1 and s2 reference to the same String object

B. s1 and s2 reference to different String objects

27 What is the output of running class C?

class A {

public A() {

System.out.println(

"The default constructor of A is invoked");

} }

class B extends A {

public B() {

System.out.println(

"The default constructor of B is invoked");

} }

public class C {

public static void main(String[] args) {

B b = new B();

} }

Page 9 of 15

A. Nothing displayed

B. "The default constructor of B is invoked"

C. "The default constructor of A is invoked""The default constructor of B is invoked"

D. "The default constructor of B is invoked""The default constructor of A is invoked"

E. "The default constructor of A is invoked"

28 Which of the statements regarding the super keyword is incorrect?

A. You can use super to invoke a super class constructor.

B. You can use super to invoke a super class method.

C. You can use super.super.p to invoke a method in superclass's parent class.

D. You cannot invoke a method in superclass's parent class.

29 Which of the following statements are true?

A. A method can be overloaded in the same class.

B. A method can be overridden in the same class.

C. If a method overloads another method, these two methods must have the same signature.

D. If a method overrides another method, these two methods must have the same signature.

E. A method in a subclass can overload a method in the superclass.

30 What is the output of the following code?

public class Test {

public static void main(String[] args) {

new Person().printPerson();

new Student().printPerson();

}

}

class Student extends Person {

public String getInfo() {

return "Student";

}

}

class Person {

public String getInfo() {

return "Person";

}

public void printPerson() {

System.out.println(getInfo());

}

}

A. Person Person

B. Person Student

C. Stduent Student

D. Student Person

31 Which of the following statements is false?

Page 10 of 15

A. A public class can be accessed by a class from a different package.

B. A private method cannot be accessed by a class in a different package.

C. A protected method can be accessed by a subclass in a different package.

D. A method with no visibility modifier can be accessed by a class in a different package.

32 __________ are referred to as heavyweight components.

A. AWT components

B. Swing components

C. GUI components

D. Non-GUI components

33 The correct order of the following three statements is ___________.

1. frame.setLocationRelativeTo(null);

2. frame.setSize(100, 200);

3. frame.setVisible(true);

A. 1 2 3

B. 1 3 2

C. 2 1 3

D. 3 2 1

34 Suppose a JFrame uses the GridLayout(0, 2). If you add six buttons to the frame, how many columns are

displayed?

A. 1

B. 2

C. 3

D. 4

35 _____________ creates a color object.

A. new Color(0, 0, 0)

B. new Color(0, 266, 0)

C. new Color(255, 255, 255)

D. new Color(1, 2, 3)

36 To add a component c to a JPanel p, use _________.

A. p.add(c)

B. p.getContentPane(c)

C. p.insert(c)

D. p.append(c)

Page 11 of 15

37 Which of the following is not an advantage of Java exception handling?

A. Java separates exception handling from normal processing tasks.

B. Exception handling improves performance.

C. Exception handling makes it possible for the caller's caller to handle the exception.

D. Exception handling simplifies programming because the error-reporting and error-handling code can be

placed at the catch block.

38 Which of the following statements are true?

A. You use the keyword throws to declare exceptions in the method heading.

B. A method may declare to throw multiple exceptions.

C. To throw an exception, use the key word throw.

D. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the

method.

39 An instance of _________ describes the errors caused by your program and external circumstances. These

errors can be caught and handled by your program.

A. RuntimeException

B. Exception

C. Error

D. Throwable

E. NumberFormatException

40 Which of the following declares an abstract method in an abstract Java class?

A. public abstract method();

B. public abstract void method();

C. public void abstract Method();

D. public void method() {}

E. public abstract void method() {}

41 Which of the following statements regarding abstract methods are true?

A. Abstract classes have constructors.

B. A class that contains abstract methods must be abstract.

C. It is possible to declare an abstract class that contains no abstract methods.

D. An abstract method cannot be contained in a nonabstract class.

E. A data field can be declared abstract.

Page 12 of 15

42 Show the output of running the class Test in the following code lines:

interface A {

}

class C {

}

class B extends D implements A {

}

public class Test extends Thread {

public static void main(String[] args) {

B b = new B();

if (b instanceof A)

System.out.println("b is an instance of A");

if (b instanceof C)

System.out.println("b is an instance of C");

}

}

class D extends C {

}

A. Nothing.

B. b is an instance of A.

C. b is an instance of C.

D. b is an instance of A followed by b is an instance of C.

43 Suppose a button jbt is placed in a frame, the coordinate of the button within the content pane of the frame is

_______.

A. (jbt.getX(), jbt.getY())

B. (jbt.x, jbt.y)

C. cannot be obtained

D. (0, 0)

44 You should override the __________ method to draw things on a Swing component.

A. repaint()

B. update()

C. paintComponent()

D. init()

45 Which of the following statements are true?

A. You may create a Graphics object using new Graphics().

B. Whenever a GUI component is displayed, its Graphics object is automatically created.

C. The paintComponent method is automatically invoked by the JVM. You should never invoke it directly.

D. Invoking repaint() causes paintComponent to be invoked by the JVM.

Page 13 of 15

46 Which of the following statements are correct?

A. You can set an image on a label, but the image is not resizable.

B. You can set an image on a button, but the image is not resizable.

C. You can draw an image on a GUI component using the drawImage method in the Graphics object. This

image is resizable.

47 Clicking the closing button on the upper-right corner of a frame generates a(n) __________ event.

A. ItemEvent

B. WindowEvent

C. MouseMotionEvent

D. ComponentEvent

E. ContainerEvent

48 Which of the following statements are true?

A. Each event class has a corresponding listener interface.

B. The listener object's class must implement the corresponding event-listener interface.

C. A source may have multiple listeners.

D. The listener object must be registered by the source object.

49 To get the x coordinate of the mouse pointer for the MouseEvent evt, you use __________.

A. evt.getX()

B. evt.getPoint().x

C. Either A or B

D. Neither A nor B

50 Analyze the following code:

public class Test implements Runnable {

public static void main(String[] args) {

Thread t = new Thread(this);

t.start();

}

public void run() {

System.out.println("test");

}

}

A. The program does not compile because this cannot be referenced in a static method.

B. The program compiles fine, but it does not print anything because t does not invoke the run() method.

C. The program compiles and runs fine and displays test on the console.

D. None of the above.

Page 14 of 15

Section II [Total 30]

You should attempt all section II (questions 2, 3, and 4)

Q(2): write a program that displays a message in a panel, and uses two buttons to move the message on the

panel to the left or right as shown in Figure 1. [12 Marks]

Q(3): (Creating four fans) Write a program that places four fans in a frame of GridLayout with two rows and two

columns, as shown in Figure 2. [12 Marks]

Q(4): Write a problem that prompts the user to enter a string and reports whether the string is a palindrome. Note

that (palindrome if it reads the same forward and backward).

[6 Marks]

Good Luck …

Figure 1 Figure 2

Page 15 of 15

Section I [Total 60]

Q(1): Multiple choice questions

Section I : Key answer for the multiple choice questions

Questions 1 2 3 4 5 6 7 8 9 10

Answer Key

Questions 11 12 13 14 15 16 17 18 19 20

Answer Key

Questions 21 22 23 24 25 26 27 28 29 30

Answer Key

Questions 31 32 33 34 35 36 37 38 39 40

Answer Key

Questions 41 42 43 44 45 46 47 48 49 50

Answer Key