0% found this document useful (0 votes)
30 views14 pages

Chapter#2 Exercises - Code Analysis-Java Oop

This document contains code analysis exercises related to defining classes in object-oriented programming. There are 26 multiple choice questions about analyzing code snippets and predicting output or errors. The questions cover topics like class and object definitions, constructors, access modifiers, and more.

Uploaded by

Ice Tech
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views14 pages

Chapter#2 Exercises - Code Analysis-Java Oop

This document contains code analysis exercises related to defining classes in object-oriented programming. There are 26 multiple choice questions about analyzing code snippets and predicting output or errors. The questions cover topics like class and object definitions, constructors, access modifiers, and more.

Uploaded by

Ice Tech
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Al-Maaref University

Faculty of Computer Science (CSC)

CSC 310 – OBJECT ORIENTED PROGRAMMING

CHAPTER 2 - DEFINING CLASSES

CODE ANALYSIS EXERCISES

I NSTRUCTOR
D R . R AYANE E L S IBAI

Exercises – Chapter 2 Page 1 of 14


CODE ANALYSIS QUESTIONS

1. What might go wrong with the following code, what would you need to do to fix it?

2. What is wrong with the following program?

3. Why the below program is showing compile time error?

Exercises – Chapter 2 Page 2 of 14


4. What is the output of the following code?

Exercises – Chapter 2 Page 3 of 14


5. Analyze the following code:

a. The program has a compile error because class A is not a public class.
b. The program has a compile error because class A does not have a default constructor.
c. The program compiles and runs fine and prints nothing.
d. The program would compile and run if you change A a = new A() to A a = new A("5").

6. Analyze the following code:

a. The program has a compile error because TempClass does not have a default constructor.
b. The program has a compile error because TempClass does not have a constructor with an int
argument.
c. The program compiles fine, but it does not run because class C is not public.
d. The program compiles and runs fine.

Exercises – Chapter 2 Page 4 of 14


7. Given the declaration Circle x = new Circle(), which of the following statements is most accurate.
a. x contains an int value.
b. x contains an object of the Circle type.
c. x contains a reference to a Circle object.
d. You can assign an int value to x.

8. Analyze the following code:

a. The program has a compile error because test is not initialized.


b. The program has a compile error because x has not been initialized.
c. The program has a compile error because you cannot create an object from the class that
defines the object.
d. The program has a compile error because Test does not have a default constructor.
e. The program has a runtime NullPointerException because test is null while executing test.x.

9. Analyze the following code:

a. The program has a compile error because the variable radius is not initialized.
b. The program has a compile error because a constant PI is defined inside a method.
c. The program has no compile errors but will get a runtime error because radius is not
initialized.
d. The program compiles and runs fine.

Exercises – Chapter 2 Page 5 of 14


10. Analyze the following code:

a. The program has a compile error because System.out.println method cannot be invoked from
the constructor.
b. The program has a compile error because x has not been initialized.
c. The program has a compile error because you cannot create an object from the class that
defines the object.
d. The program has a compile error because Test does not have a default constructor.

11. Analyze the following code:

a. Since x is private, it cannot be accessed from an object foo.


b. Since x is defined in the class Foo, it can be accessed by any method inside the class without
using an object. You can write the code to access x without creating an object such as foo in
this code.
c. Since x is an instance variable, it cannot be directly used inside a main method. However, it
can be accessed through an object such as foo in this code.
d. You cannot create a self-referenced object; that is, foo is created inside the class Foo.

Exercises – Chapter 2 Page 6 of 14


12. Analyze the following code:

a. The program has a compile error because it does not have a main method.
b. The program will compile, but you cannot create an object of Circle with a specified radius.
The object will always have radius 0.
c. The program has a compile error because you cannot assign radius to radius.
d. The program does not compile because Circle does not have a default constructor.

13. What output is produced by the program?

a. Compiler Error
b. Object containing 23
c. Object containing 33
d. Object containing 34
e. None of the above

Exercises – Chapter 2 Page 7 of 14


14. What output is produced by the program?

a. Compiler Error
b. Object containing 23
c. Object containing 33
d. Object containing 34
e. None of the above

15. What output is produced by the program?

a. Compiler Error
b. Object containing 0, 0.0, false
c. Object containing 0, 0.0, true
d. None of the above

Exercises – Chapter 2 Page 8 of 14


16. What output is produced by the program?

a. Compiler Error
b. Object containing 0, 0.0, false
c. Object containing 0, 0.0, true
d. None of the above

17. What output is produced by the program?

a. Compiler Error
b. 2
c. 5
d. None of the above

Exercises – Chapter 2 Page 9 of 14


18. What output is produced by the program?

a. Compiler Error
b. 2
c. 5
d. None of the above

19. What is the output is produced by the program?

a. Compiler Error
b. 2
c. 5
d. 10
e. None of the above

Exercises – Chapter 2 Page 10 of 14


20. What output is produced by the program?

a. Compiler Error
b. 2
c. 5
d. 10
e. None of the above

21. What is the output of the following code?

Exercises – Chapter 2 Page 11 of 14


22. What is the output of the following code?

23. What is the output of the following code?

Exercises – Chapter 2 Page 12 of 14


24. What is the output of the following code?

Exercises – Chapter 2 Page 13 of 14


25. What is the output?

26. What is the output:

Exercises – Chapter 2 Page 14 of 14

You might also like