0% found this document useful (0 votes)
18 views8 pages

Chapter 3 - Exercises

The document contains exercises and questions about defining classes in Java. It includes multiple choice, true/false, open answer and coding questions about static variables, static methods, wrapper classes and other class-related concepts. Coding exercises at the end provide examples to test class definitions and methods.

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)
18 views8 pages

Chapter 3 - Exercises

The document contains exercises and questions about defining classes in Java. It includes multiple choice, true/false, open answer and coding questions about static variables, static methods, wrapper classes and other class-related concepts. Coding exercises at the end provide examples to test class definitions and methods.

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
You are on page 1/ 8

Al-Maaref University

Faculty of Computer Science (CSC)

CSC 310 – OBJECT ORIENTED PROGRAMMING

CHAPTER 3 – DEFINING CLASSES II

EXERCISES

I NSTRU CTOR
D R . A B ED EL S AFADI

Exercises – Chapter 3 Page 1 of 8


MULTIPLE CHOICE QUESTIONS

1. A static method is one that can be used with a _____________.


a. instance variable
b. local variable
c. global variable
d. the class name as a calling object

2. Static variables are often used:


a. in arithmetic expressions
b. to communicate between objects
c. within looping structures
d. all of the above

3. Only ______ copy/copies of a static variable are available to objects of a class.


a. one
b. two
c. three
d. none of the above

4. All of these are methods of Java’s Math class except:


a. pow
b. min
c. random
d. toString

5. All of the following are wrapper classes except:


a. String
b. Integer
c. Character
d. Double

6. Converting from a value of primitive type to a corresponding object of its associated wrapper
class is called:
a. Boxing
b. Unboxing
c. Converting
d. Reinstantiating

7. The conversion from an object of a wrapper class to a value of its associated primitive type is
called:
a. Boxing
b. Unboxing
c. Converting
d. Reinstantiating

Exercises – Chapter 3 Page 2 of 8


TRUE/FALSE QUESTIONS

1. A main method can be placed inside a class definition.


2. You may use methods of the Math class without an import statement.
3. Wrapper classes provide a class type corresponding to each of the primitive types so that you can
have class types that behave somewhat like primitive types.
4. All versions of Java support automatic boxing.
5. Wrapper classes are provided for all primitive Java types, except Boolean.
6. Primitive types are reference types.

OPEN ANSWERS QUETIONS

1. What is the difference between a static variable and an instance variable?


2. Can you use an instance variable (without an object name and dot) in the definition of a static
method of the same class? Can you use an instance variable (without an object name and dot) in
the definition of a non-static (ordinary) method of the same class?
3. Can you use a static variable in the definition of a static method of the same class? Can you use a
static variable in the definition of a non-static (ordinary) method of the same class?
4. Can you use the this parameter in the definition of a static method?
5. Following the style guidelines given in the book, when should a static variable be marked private?
6. What do static methods and static variables have in common? After all, they are both called static,
so it sounds like they have something in common.
7. Write a statement that creates and initializes a static variable named salesTax to 7.59.
8. Write a statement that creates a constant variable named TAX_RATE. The tax rate is 8.25%.
9. Write ONE Java statement that computes and displays the value of 25.
10. Write ONE Java statement that computes and displays a random number between 1 and 25.
11. Define boxing and unboxing.
12. When used with objects, what is the equality ( == ) operator really comparing?
13. What is the purpose of Java’s wrapper classes?

CODE ANALYSIS QUETIONS

1. Check the following code and answer the below questions.

Exercises – Chapter 3 Page 3 of 8


a. What is written to the standard output as the result of executing the following statements?
b. Is it necessary to instantiate the class Calculus? Clarify your answer.
c. How can you prevent anyone to instantiate the class Calculus?

2. What is the output of the following code?

3. What is the output of the following code?

Exercises – Chapter 3 Page 4 of 8


4. What is the output of the following code?

5. What is the output of the following code?

Exercises – Chapter 3 Page 5 of 8


6. What is the output of the following code?

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

8. Analyze the following code.

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.
Exercises – Chapter 3 Page 6 of 8
9. What is the output of the following code?

a. f2.i is 1 f2.s is 1
b. f2.i is 1 f2.s is 2
c. f2.i is 2 f2.s is 2
d. f2.i is 2 f2.s is 1

10. What is the output of the following code?

a. f3.i is 1 f3.s is 1
b. f3.i is 1 f3.s is 2
c. f3.i is 1 f3.s is 3
d. f3.i is 3 f3.s is 1
e. f3.i is 3 f3.s is 3

Exercises – Chapter 3 Page 7 of 8


11. Analyze the following code:

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.

CODING EXERCISES II

PROGRAM 1

1. Create a class called “SavingsAccount” with the following specifications:


a. Use a static variable annualInterestRate to store the annual interest rate for all account
holders. The default value is 0.0.
b. Each object of the class has a private instance variable savingsBalance indicating the
amount the saver currently has on deposit. The default value is 0.0.
c. Provide a static method called setInterestRate that sets the annualInterestRate to a new
value.
d. Provide a method called calculateMonthlyInterest to calculate the monthly interest by
multiplying the savingsBalance by annualInterestRate divided by 12.
i. This interest should be added to savingsBalance.

2. Write a program to test class SavingsAccountTest, as follows:


a. Instantiate two savingsAccount objects, called saver1 and saver2, with balances of
$2000.00 and $3000.00, respectively.
b. Set the value of annualInterestRate to 4%, then, calculate the monthly interest and print
the new balances for both savers.
c. Set the value of annualInterestRate to 5%, then, calculate the new month’s interest and
print the new balances for both savers.

Exercises – Chapter 3 Page 8 of 8

You might also like