You are on page 1of 7

Page 1 of 7 1/12/2017 1:17 PM

Quiz 3 [IT112]

_____________________:‫الرقم الدراسي‬ __________________________________ : ‫االسم‬

Q 1. What are the instance variables? give example (2 points)

.............................................................................................................................................
.............................................................................................................................................
p.35-36
.............................................................................................................................................
.............................................................................................................................................

Q 2. Multiple Choice Questions (13 points) .Each question has EXACTLY one correct answer.

1-What is the meaning of the instance Method?


A. Static methods of the same class.
B. Method does not exist in memory.
C. Methods those defined in the same class.
D. None of the above.

2-A constructor …………………………………….


A. Must have the same name as the class it is declared within.
B. Is used to create objects.
C. Must have no explicit return type
D. A and B
E. A, B and C

3-Which of the following may be part of a class definition?


A. Instance variables
B. Instance methods
C. Constructors
D. All of the above
E. None of the above

4 - Which is a technique in Java in which a class can have any number of constructors
that differ in parameter lists?
A- Constructor overloading
B- Method overloading
C- Operator overloading
D- None of the above
Page 2 of 7 1/12/2017 1:17 PM

5- Consider public class MyClass {


private int x;}
To instantiate MyClass, you would write?
A. MyClass mc = new MyClass;
B. MyClass mc = MyClass();
C. MyClass mc = MyClass;
D. MyClass mc = new MyClass();
E. It can't be done. The constructor of MyClass should be defined.

6 - What is function overloading?


A - Methods with same name but different parameters.
B - Methods with same name but different return types.
C - Methods with same name, same parameter types but different parameter names.
D - None of the above.

7-The following program is an example for?


class Student{
int id;
String name;
void display(){System.out.println(id+" "+name); }
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display(); }
}
A. Constructor by Parameter
B. Default Constructor
C. Overloading Constructor
D. None of the above

8-Which of the following is a key component of object oriented programming?


A. Inheritance
B. Encapsulation
C. Polymorphism
D. All of the above

9- Which of these is TRUE of the relationship between objects and classes?


A. A class is an instance of an object.
B. An object is a variable.
C. An object is an instance of a class.
D. An object is not an instance of a class.

return
10-Which of the following is NOT a valid Data data type?
type?
A. void
Page 3 of 7 1/12/2017 1:17 PM

B. int
C. String
D. static

11-Which of the following is a characteristic of an overloaded method?


A. It must have a fixed number of parameters
B. It could never be a constructor
C. It shares a name with another method in the same class
D. It always returns void

12-Which of the following is a true statement regarding a constructor?


A. It has no return type
B. Its return type changes based on how you write it
C. its return type is void
D. its return type is int

13. Consider class BankDetails is

public class BankDetails {


private int accountNumber;
private double Balance;

public BankDetails(int Number, double balance){


accountNumber = Number;
Balance = balance;
System.out.print(“500“);
}
public double getCurrentSalary(){return Balance;}

public static void main(String[] args) {


BankDetails bankDetails = new BankDetails(14523,500);
System.out.print(bankDetails .getCurrentSalary());
}}

A. 14523 500
B. 500
C. 500 500
D. 14523 500 500
E. 14523 500 Number balance
F. 14523 Number 500 balance
Page 4 of 7 1/12/2017 1:17 PM

Q 3. A- What is the output of the following code (5 points)

Product.java
public class Product {
private int productId;
private String productName;
private String productDescription;
private double price;
public Product(int Id, String Name, String description, double Price)
{
productId = Id;
productName = Name;
productDescription = Description;
price = Price;
}
public int getProductId(){return productId;}
public void setProductId(int product){productId = product;}
public String getProductName(){return productName; }
public void setProductName(String Name){productName =Name;}
public String getProductDescription(){return productDescription; }
public void setProductDescription(String Des){
productDescription = Des; }
public double getPrice() { return price; }
public void setPrice(double Price) {price = Price; }

public String toString(){


return "price= " + price + ", productDescription= "
+ productDescription + ", productId= " + productId
+ ",productName= " + productName; }

ProductMain.java
import java.io.Scanner;
public class ProductMain {
public static void main() {

Product p1=new Product(555, "Monitor", "15 inch", 4000.00);


Product p2=new Product(2222, "Monitor", "17 inch", 5000.00);
Product p3=new Product(3333, "Del", "Laptop", 4000.00);
Product p4=new Product(6666, "Mouse", "Optical Mouse", 200.00));
Scanner sc=new Scanner (System.in);
int i=0;
while(i<10)
{
i++;
System.out.println("1 : Display All Products");
System.out.println("2 : Search product by Description ");
System.out.println("3 : Find the Max of price");
int ch=sc.nextInt();
switch(ch)
{
case 1 :
System.out.println("Display All Products: ");
System.out.println("Product 1"+p1.toString());
Page 5 of 7 1/12/2017 1:17 PM

System.out.println("Product 2"+p2.toString());
System.out.println("Product 3"+p3.toString());
System.out.println("Product 4"+p4.toString());
break;

case 2 :
String Description=sc.next();
if(p1.getProductDescription()== Description)
System.out.println("Found Product is:"+p1.toString());
else if(p2.getProductDescription()== Description)
System.out.println("Found Product is:"+p2.toString());
else if(p3.getProductDescription()== Description)
System.out.println("Found Product is:"+p3.toString());
else if(p4.getProductDescription()== Description)
System.out.println("Found Product is:"+p4.toString());
else System.out.println(Description +"Not Found !");

break;
case 3 :
double max=p1.getPrice();
if(p2.getPrice()>max) max=p2.getPrice;
else if(p3.getPrice()>max) max=p3.getPrice;
else if44(p4.getPrice()>max) max=p4.getPrice;
System.out.println("The max price of product is :"+max);
break;

default : System.out.println("Invalid Option");


System.exit(0);
}// switch
}// wh-ile
}// main
}// class

Consider in the Run, user choose:


ch=3
ch=2 Description =”Laptop”
ch=1
ch=2 Description =”Desktop”
ch=4

Ch=3
……………………………………………………………………………………………………………………………………………………
The max price of product is :5000.00
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
ch=2 Description =”Laptop”
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
Found Product is:price=4000.00 productDescription=Laptop
……………………………………………………………………………………………………………………………………………………
productId=3333 productName=Del
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
Page 6 of 7 1/12/2017 1:17 PM

Ch=1
price=4000.00 productDescription=15 inch productId=555 productName=Monitor
……………………………………………………………………………………………………………………………………………………
price=5000.00 productDescription=17 inch productId=3333 productName=Monitor
……………………………………………………………………………………………………………………………………………………
price=4000.00 productDescription=Laptop productId=3333 productName=Del
……………………………………………………………………………………………………………………………………………………
price=200.00 productDescription=Optical Mouse productId=6666 productName=Mouse
……………………………………………………………………………………………………………………………………………………
ch=2 Description =”Desktop”
……………………………………………………………………………………………………………………………………………………
Desktop Not Found !
……………………………………………………………………………………………………………………………………………………
Ch=4
……………………………………………………………………………………………………………………………………………………
Invalid Option
………………………………………………………………………………………………………...............................................

Q 4. A- Correct the Error and find the output from the following code (5 points)

public class Employee {


private String name; // Employee's name
private int idNumber; // ID number
private String department; // Employee's department
private String position; // Job title
private int years; //year of work
public Employee(){
name = ""; year=0;
idNumber = 0;
department = ""; , int y
position = "";}
public Employee(String n, int id, String dept, String pos){
name = n;
idNumber = id; year=y;
department = dept;
position = pos;}
public void setName(String n) { name = n;}
public void setIdNumber(int num) {idNumber = num; }
public void setDepartment(String d) { department = d;}
public void setPosition(String p) { position = p; }
public int getYear() {return year;}
public String getName() {return name; }
public int getIdNumber() { return idNumber;}
public String getDepartment() { return department;}
public String getPosition() {return position; }
}// class

public class employeeTest{


15
public static void main(String[] args){
Employee box = new Employee("Eric Meyers", 48899, "Accounting", "Vice Pres", "15");
Employee box1 = new Employee("Anthony Jones", 30119, "IT", "Programmer", "8");
Employee box2 = new Employee("Carlos Rodgers", 81674, "Manufacturing", "Engineer", "12");
System.out.println(" Name ID Number Department Position Yearsworked ");
System.out.println("___________________________________________");
Page 7 of 7 1/12/2017 1:17 PM

System.out.print(" " + box.getName() + " " + box.getIdNumber() + " "+


box.getDepartment());
System.out.println(" " + box.getPosition());+" "+box.getYear());
System.out.print(" " + box1.getName() + " " + box1.getIdNumber() + " "+
box1.getDepartment());
System.out.println(" " + box1.getPosition());+" "+box1.getYear());
System.out.print(" " + box2.getName() + " " + box2.getIdNumber() + " "+
box2.getDepartment());
System.out.println(" " + box2.getPosition( ) );+" "+box2.getYear());
}//main

}// class

……………………………………………………………………………………………………………………………………………………
Name ID Number Department Position Yearsworked
……………………………………………………………………………………………………………………………………………………
___________________________________________
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
Eric Meyers 48899 Accounting Vice Pres 15
……………………………………………………………………………………………………………………………………………………
Anthony Jones 30119 IT Programmer 8
……………………………………………………………………………………………………………………………………………………
Carlos Rodgers 81674 Manufacturing Engineer 12
……………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………...............................................

……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………...............................................

You might also like