0% found this document useful (0 votes)
35 views5 pages

CP2 MidExam

Cp2
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)
35 views5 pages

CP2 MidExam

Cp2
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/ 5

Computer Programming II: ITCS 2301

Mid-Term Examination
DATE :18/7/2024

20222374:‫الرقم الجامعي‬ ‫ يحيى بشير عودة أبوصفية‬:)‫االسم (رباعي‬

:‫تعليمات‬

.‫ اكتب اسمك ورقمك الجامعي في اعلي الملف‬-

‫ التسليم سوف يكون نفس الملف بعد وضع اجابتك فيه و التسليم عبر الموودل‬-

1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
A D B B A C E E C D
Q#1: Choose the correct answer:
__________ variables and methods can be accessed and called without using the objects.
1.
A. static B. private C. final D. public
Analyze the following code: A. The variable k is private and therefore
public class Test { cannot be accessed in the main .
private int k; B. The program compiles and runs fine.
public static void main(String[] args) { C. The variable k is not initialized and
2. int a=0; therefore causes errors.
System.out.println(k);
D. k is non-static and it cannot be
} referenced directly in a static context in
} the main.
Analyze the following code: A. The pages variable cannot be accessed
public class Book { in the Main class of the same package .
int pages; } B. The program compiles and runs fine.
class Main{ C. The pages variable is not initialized and
3.
public static void main(String[] args) { therefore causes errors.
Book b = new Book(); D. pages is non-static and it cannot be
System.out.println(b.pages); } referenced through the b object in a
} static context in the main
True or False: In Java, encapsulation helps to achieve data hiding by making the fields of a
4. class public and providing access to them through private methods.
A. True B. False
True or False: In Java, the String class is immutable, meaning that once a String object is
5. created, its value cannot be changed.
A. True B. False
What is the output of the following program? A. 100 Tesla
class Car {
static int speed; B. 100 KIA
String model; C. 120 Tesla
public static void main(String args[]) {
Car c1 = new Car(); D. 120 KIA
6.
Car c2 = new Car();
c1.speed= 100; c1.model = "Tesla"; E. Compiler error
c2.speed= 120; c2.model= "KIA";
System.out.println(c1.speed);
System.out.println(c1.model); }
}

|Page1
Consider the following class definition: A. value = i;
public class MyClass {
private int value; B. i = value ;
public void setValue(int i){ /code/ } C. this.value = i ;
7. // Other methods…
} D. return value;
What could you write for the implementation of setValue?
E. A and C
Which of the following statements are true about A. 1, 2 and 5 only.
constructors?
B. 1, 2 and 3 only.
1. A class can have more than one constructor.
2. The constructors can be overloaded.X`` C. 1, 3 and 5 only.
8. 3. If no constructor is defined in a class, the compiler
D. 1, 2, 3, 4 and 5.
will define a default constructor.
4. Constructors can be declared using static keyword E. 1, 2, 3, 5 only.
5. Constructors cannot return values.
F. 1 and 5 only.
A. calls the method named "this" and defined
in the current class.
B. calls the method named "super" and
The instruction "this( );" does which of the
defined in the current class’ super class.
9. following?
C. calls the constructor defined in the current
class.
D. calls the constructor defined in the current
class’ super class.
What is the output of the following program? A. 0 0
public class Foo {
private int x; B. compiler error on line 1
public static void main(String[] args) {
10. Foo f = new Foo() ; //line 1 C. compiler error on line 3
int y; //line 2 D. compiler error on line 4
System.out.println(f.x); //line 3
System.out.println(y); //line 4 E. compiler error on line 3 and 4
}}

Q#2:
a. Explain clearly what the terms composition and aggregation mean in the context of
object-oriented programming (OOP).

Aggregation is an relationship when there is an object that consist of several


other objects , such as the car object and the engine object , which is the
relationship of composition and another example , such as the car object to the
passenger object which is a relationship of a aggregation ,
And the different between them is that the composition is a strong relationship If
the 'Car' object is destroyed, the contained objects are also destroyed.
Otherwhise
But the aggregation is a every objects are self-contained, if the 'Car' object is
destroyed, the contained objects are not destroyed.

|Page2
b. Explain what class inheritance is, using a simple example to illustrate your solution

Inheritance is when we have many objects that have common features among
them and we want to avoid repetition in writing them .
Here we use inheritance by creating a super class , its known as the parent class
and make these classes ( subclasses ) that have common characteristics .
inhert common characteristics such as animal class and dog class , so that the
dogs inherits the character eating from the animal class .

|Page3
Q#3:
a- Create a class to represent a Student . Include the following private object
variables:
- Name of the Student (String)
- The average of the student (double)
Include a constructor with arguments to create a student, get public
methods and a public method to print the student data.

b - Write a Test program to read data to create 4 student objects and then
print the name of the student whose the highest average (hint: you can use a
Student object array in your solution).

|Page4
Good Luck!

|Page5

You might also like