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

Code Analysis Exercises for 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Code Analysis Exercises for 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] 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

Common questions

Powered by AI

A compile-time error occurs if a public class is required for the Java program to be executed as the entry point in a file is absent. This happens because, in Java, the file name must match the single public class it contains. If there is no public class or if the public class definition is missing, the Java Virtual Machine cannot find the executable class, resulting in a compilation error. This ensures that each Java file has an organized entry point for execution .

Constructors play a crucial role in object-oriented programming by setting the initial state and properties of an object at the time of its creation. They provide a way to enforce constraints and initialize fields with proper values, ensuring that the object starts its life cycle in a valid state. Without constructors, objects would default to uninitialized states, potentially leading to unexpected behaviors. Constructors can also overload to offer multiple versions for diverse initialization requirements .

It is necessary to initialize variables before use in a Java program because uninitialized variables lead to compile-time errors. Java does not allow the use of variables that may not have been assigned a value to prevent undefined behavior and potential runtime exceptions. If a variable is used before being initialized, the compiler flags it with an error such as 'variable might not have been initialized' as a safeguard against unpredictable program operation .

Encapsulation in Java controls access to class members like instance variables to protect data integrity and restrict unauthorized access. Instance variables, such as 'x', are typically declared as private. This means they can only be accessed indirectly through methods within the same class, thus maintaining encapsulation. For instance, if 'x' is a private variable within a class 'Foo', it cannot be accessed directly from outside the class. Internal methods or getter and setter methods must be used to access or modify its value, illustrating encapsulation .

A 'NullPointerException' in Java occurs when a program attempts to use an object reference that has not been initialized to point to any object, effectively referring to 'null'. This exception can be avoided by ensuring all object references are properly initialized before use. For example, if a variable 'test' of type 'Test' is declared but not instantiated, accessing 'test.x' will result in a 'NullPointerException' since 'test' does not point to any object. This can be prevented by initializing the object using 'test = new Test()' before trying to access its fields .

Defining a constant inside a method in Java challenges the program's readability and access to the constant outside the method scope. Constants defined inside a method are only accessible within that method, and they cannot retain values beyond the method lifecycle. This scope limitation means that each time the method is executed, the constant needs to be redefined, which can be inefficient if the constant is needed across multiple methods or classes. This could lead to a compilation error if attempted to be accessed from outside the method .

Compile errors and runtime errors differ significantly in detection and occurrence in Java. Compile errors are detected at the time of code compilation when the Java compiler analyzes the source code for syntactical correctness and adherence to language rules. Examples include syntax errors or type mismatches. Runtime errors, in contrast, only appear during program execution, often due to logical errors or invalid operations, such as dividing by zero or dereferencing null objects. Thorough code testing can reduce runtime errors, whereas compile errors must be resolved for successful code compilation .

Invoking 'System.out.println' from a constructor may not be possible if certain structural rules are violated, such as if the method being called belongs to a class that has not been fully instantiated, leading to a state where some members may not yet be initialized. In rare cases, if object initialization depends on configuration outside the constructor, trying to execute such a print statement can result in erratic behavior or errors. Constructors should focus on initialization, ensuring that calling methods not appropriate during instantiation due to dependency on fully initialized object state may cause logic errors if invoked prematurely .

A lack of a default constructor in a class affects code in that if the program tries to instantiate a class without providing any arguments, it will throw a compile-time error if no default constructor is defined. For example, if a class TempClass does not have a default constructor, attempting to create an instance using 'new TempClass()' would result in a compile-time error because the compiler cannot find a constructor that matches the object creation. This occurs because Java only provides a default constructor when no other constructors are explicitly defined .

The assignment statement 'Circle x = new Circle()' affects object allocation and reference by creating a new Circle object in memory and assigning the memory address of this object to the reference variable x. Specifically, x holds a reference to the newly created Circle object, not the object itself. This means x acts as a pointer to the location in memory where the Circle object is stored. Hence, any operations on x allow the program to manipulate the actual Circle object in memory .

You might also like