Java Assignment Answer
Java Assignment Answer
Date:____________________
Total Weight:- 20 %
GENERAL DIRECTIONS
1
GAGE COLLEGE DEPARTMENT OF COMPUTER SCIENCE OBJECT ORIENTED
PROGRAMMING FINAL EXAM
Part One: Say true if the statement is correct false if the statement is incorrect. (10 pts.)
False
_______ 1.A way of simplifying complex reality is called encapsulation
True
_______ 2.A class is a blue print or template that defines states and behavior common to all objects of a
certain kind.
True
_______ 3.A block statement is a group of zero or more statement between balanced braces and can be
used anywhere a single statement is allowed.
True 4. Object is instance of a class
_______
True 5. Java provides a default constructor automatically if the programmer does not define it.
_______
True 6. Default, parameterized and copy constructors are the three types of constructor.
_______
True
_______ 7. In the concept of inheritance the super class does not know the existence of the sub class but
the sub class knows the existence of the super class
False 8. Java does not support multiple inheritances that a derived class can have only one parent class.
_______
True 9. Constructors are used for initializing the instance, variables of an object
_______
True 10. Interface is useful when there is a parent and child class relationships to inherit the property of
_______
parent class.
2
Part two: Workout questions
1) Following are the difference between constructor and method.
Constructor does not return any value where the method may/may not return a value.
In case constructor is not present, a default constructor is provided by java compiler. In the case
of a method, no default method is provided.
Constructor should be of the same name as that of class. Method name should not be of the
same name as that of class.
2) Abstract Class
An abstract class is a class that is declared abstract — it may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed. An abstract class may have static
fields and static methods. When an abstract class is subclassed, the subclass usually provides
implementations for all of the abstract methods in its parent class. However, if it does not, then the
subclass must also be declared abstract.
An abstract method is a method that is declared without an implementation (without braces and
followed by a semicolon)
3) Object − Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a
class.
1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
R e f Private C struct r
If you make any class constructor private, you cannot create the instance of that class from
outside the class. For example:
1. class A{
2. private A(){}//private constructor
3. void msg(){System.out.println("Hello java");}
4. }
5. public class Simple{
6. public static void main(String args[]){
7. A obj=new A();//Compile Time Error
8. }
9. }
Note: A class cannot be private or protected except nested class.
2) Pr tected
The protected access modifier is accessible within package and outside the package but
through inheritance only.
The protected access modifier can be applied on the data member, method and constructor.
It can't be applied on the class.
In this example, we have created the two packages pack and mypack. The A class of pack
package is public, so can be accessed from outside the package. But msg method of this
package is declared as protected, so it can be accessed from outside the class only through
inheritance.
1. //save by A.java
2. package pack;
3. public class A{
4. protected void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }
Output:Hello
3) Pub ic
The public access modifier is accessible everywhere. It has the widest scope among all
other modifiers.
1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2.
3. package mypack;
4. import pack.*;
5.
6. class B{
7. public static void main(String args[]){
8. A obj = new A();
9. obj.msg();
10. }
11. }
Output:Hello
import java.util.Scanner;
String set1 =
" 1 3 5 7\n" +
" 9 11 13 15\n" +
" 17 19 21 23\n" +
" 25 27 29 31";
String set2 =
" 2 3 6 7\n" +
" 10 11 14 15\n" +
" 18 19 22 23\n" +
" 26 27 30 31";
String set3 =
" 4 5 6 7\n" +
" 12 13 14 15\n" +
" 20 21 22 23\n" +
" 28 29 30 31";
String set4 =
" 8 9 10 11\n" +
" 12 13 14 15\n" +
" 24 25 26 27\n" +
" 28 29 30 31";
String set5 =
" 16 17 18 19\n" +
" 20 21 22 23\n" +
" 24 25 26 27\n" +
" 28 29 30 31";
int day = 0;
// Create a scanner
if (answer.equals("Y"))
day += 1;
if (answer.equals("Y"))
day += 2;
if (answer.equals("Y"))
day += 4;
if (answer.equals("Y"))
day += 8;
if (answer.equals("Y"))
day += 16;
System.out.println("\nYour birthday is " + day + "!");
}
}