1. Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune. 37
Principles of Programming Languages
CS 20105 – SE – E
Home Assignment 3
Assignment Date: 15th March 2012 Submission Date: 20th March 2012
Answer all 8 questions:
1. Write a complete Java code to print “Home Assignment 3” on console.
Specifically mention the file name and extension and the steps to compile and
execute your code.
2. What will be the output of the following code?
//Room.java
public class Room {
int i;
public Room(int i) {
this.i = i;
}
public void print() {
System.out.println(i);
}
}
//TestRoom.java
public class TestRoom {
public static void main(String[] args) {
Room r = new Room();
r.print();
}
}
3. Look at the following organization of classes:
- there are two packages, “vit.cse.se” and “vit.cse.fe”
- in “vit.cse.se” there are two classes, A and B
- in “vit.cse.fe” there are two classes, C and D
- class A has
- a private field: private int i;
Page 1 of 2
2. - another field: int j;
- a protected field: protected int k;
- a public field: public int m;
- class D extends class A
Based on the above description, which of the following statements are true in
Java:
a) Any method defined in class A can access any of the fields i, j, k and m
b) Any method defined in class A can access any of the fields j, k and m but
not i since i is private
c) Any method in class C can access m, k and j of A
d) Any method in class B can access m and j in A
e) Any method in class D can only access m of A since that is the only public
field in A
4. Write complete Java code for the following classes:
- class Vehicle
- class TwoWheeler
- class FourWheeler
- class Scooter
- class Car
Use inheritance and/or composition wherever appropriate. For fields and
methods, use common sense and make whatever assumptions necessary. Also
mention what are the different types of inheritance you have used in this
example.
5. Explain the signature of “main”method in Java.
6. True or False
a) Java supports multiple inheritance
b) Abstract class can contain some method implementations
c) Interface can be instantiated
d) A class can “extend” multiple classes
e) A class can “implement” multiple interfaces
7. What is a “user defined exception”? Write Java code for defining a user-
defined exception and a demo code for throwing and catching your exception.
8. Give an example each of multi-level inheritance and hierarchical inheritance.
Page 2 of 2