Part I: Comprehension of java programming concepts (Total 16):
Part I: Comprehension of java programming concepts (Total 16):
Answer the following questions in a paragraph using full sentences.
1. Define the term inheritance in java and give an example.(4 points).
Answer
The process by which a class called child class or base class inherits properties from derived or parent class is known as inheritance.
Inheritance helps in code reusability as well as increases the functionality.
The child class thus created can add its own features.
For Example
Class Person
{
String name;
Person(String name)
{
this.name=name;
}
void display()
{
System.out.println(“Name:”+name);
}
}
Class Doctor
{
double fees;
Doctor(String name,double fees)
{
super(name);
this.fees=fees;
}
void displayinfo()
{
super.display();
System.out.println(“\nDoctor Fees:”+fees);
}
}
In the above example Person is the parent class. Doctor is child class. Every Doctor has a name. So it inherits the features from Person class and adds its own functionalities like(fees).
2. List two differences and one similarity between an interface and an abstract class (6
Points).
Differences
1) Abstract class is a class which contains one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods.
A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present.
2) An Interface can only have public members whereas an abstract class can contain private as well as protected members.
3) Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses
Similarities:
Neither Abstract classes nor Interface can be instantiated (objects cannot be created).
3. Describe the Java Collections Framework. Explain the differences between the Set, List and Map interfaces referring to the elements/objects that they store. (6 points)
Ans
Java collections framework consists of interfaces and classes that helps in managing group of objects.
List interface stores the sequence of elements. The elements in the list can be inserted and accessed by index. List may contain duplicate elements and has methods of its own.
Declaration: interface List
Set Interface defines a set and declares the behavior of collection that doesn’t allow duplicate elements.
Declaration: interface Set
Map interface defines the character and nature of map.It maps unique keys to values.A key is regarded as a object which is used to retrieve the value at a later date.
Declaration: interface Map
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 3 of 15
Part II Fill in the blanks (Total 20 points):
Complete each sentence or statement with the missing word(s)
Type in the missing word(s) in the given box, , to complete each statement.
1. (2 pts)The constructor of a derived class can access the constructor of its base class by
using the java reserved word(keyword) super .
2. (2 pts)Late or Dynamic binding is when the meaning of a method invocation is not bound to it until the program is run.
3. (2 pts) Adding the final modifier to a method heading prevents derived classes from
overriding the method.
4. (2 pts) A(n) specifies headings for methods that must be defined by an
implementing abstract class.
5. ( 2 pts) In Java, every class is a descendant of the Object class
6. ( 2 pts) Object-oriented programming allows you to derive new classes from existing
classes. This is called inheritance.
7. (2 pts) casting may be used to convert an object of one class type to another within the
inheritance hierarchy.
8. (1 point each) A(n)Abstract class can be extended with the condition of providing
implementations for all of the methods of the class.
9. (2 pts) A(n)container is a kind of component that can have other components added to it.
10. ( 2 pts) You can refer to any member of the current object from within an instance
method or a constructor by using the java this keyword .
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 4 of 15
Part III: Multiple Choice Questions(Total 28 pts)
Identify the letter of the choice that best completes the statement or answers the question.
Type the letter “X” in the given box, , to indicate the selection. (2 points each).
1.When comparing two objects to see if they have the same contents, one should
a. use the = operator
b. use the == operator
c. define an equals method in the class and use it
d. use the equals method to see if they have the same variable name
Ans d
2. The term overloaded refers to
a. an error where more than one method matches a method call
b. two or more methods in the same class that have the same name
c. method calls where there are two many actual parameters
d. two or more methods in different classes that have the same name
Ans b
3. Suppose Child is a derived class of Parent and doStuff() is a private method in the class
Parent. Which of the following is true?
a. doStuff() may be used in the definition of methods in Child.
b. doStuff() may not be used in the definition of methods in Child.
c. In some, but not all cases, doStuff() may be used in the definition of methods in Child..
d. none of the above
Ans b
4. Which statement best describes overriding a method?
a. Methods in a base class and derived class have the same name but different visibility
modifiers.
b. Methods in a base class and derived class have the same name and have the same
number and types of parameters.
c. Methods in a base class and derived class have the same name but have different return
types.
d. Methods in a base class and derived class have the same name but different number or
types of parameters.
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 5 of 15
Ans b
5. Assume that Sub1 and Sub2 are both subclasses of class Super.
Given the declarations:
Super super = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();
Which statement best describes the result of attempting to compile and execute the following
statement:
super = sub1;
a. Compiles and definitely legal at runtime
b. Does not compile
c. Compiles and may be illegal at runtime
Ans a
6. For the following code:
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Super();
System.out.print( sup.index + "," );
sup.printVal();
}
}
What will be printed to standard output?
a. The code will not compile.
b. The code compiles and "5, Super" is printed to standard output.
c. The code compiles and "5, Sub" is printed to standard output.
d. The code compiles and "2, Super" is printed to standard output.
e. The code compiles and "2, Sub" is printed to standard output.
f. The code compiles, but throws an exception.
Ans b
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 6 of 15
7. What will happen when you attempt to compile and run the following code. Classes are
saved in separate files.
public class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
public class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}
a. Compile time error complaining that Base.amethod is private
b. Runtime error complaining that Base.amethod is private
c. Output of "Base.amethod"
d. Output of "Over.amethod"
Ans d
8. Which of the following statements is true?
a. An interface can only contain method(s) and not variables
b. Interfaces cannot have constructors
c. A class may extend only one other class and implement only one interface
d. A class accesses an interface via the keyword uses
Ans b
9. An abstract class
a. is a class which cannot be instantiated
b. is a class which has no methods
c. is a class which has only abstract methods
d. is a class which has only overridden methods
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Ans a
Page 7 of 15
10) Which of the following statements is false?
a. If a class has any abstract methods it must be declared abstract itself.
b. All methods in an abstract class must be declared as abstract
c. When applied to a class, the final modifier means it cannot be sub-classed
d. abstract and interface are Java keywords
Ans b
11. A catch-block
a. is a special method used in exception handling
b. allows the user to specify what will happen next in the program
c. is not allowed in the same method as a try-block
d. contains code that is executed when an exception occurs
Ans a
12. Given the following code
import java.io.*;
public class Ppvg{
public static void main(String argv[]){
Ppvg p = new Ppvg();
p.fliton();
}
public int fliton(){
try{
FileInputStream din = new FileInputStream("Ppvg.java");
din.read();
}catch(IOException ioe){
System.out.println("flytwick");
return 99;
}finally{
System.out.println("fliton");
}
return -1;
}
}
Assuming the file Ppvg.java is available to be read correctly which of the following
statements is true if you try to compile and run the program?
a. The program will run and output only "flytwick"
b. The program will run and output only "fliton"
c. The program will run and output both "fliton" and "flytwick"
d. An error will occur at compile time because the method fliton attempts to return two
values
Ans b
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 8 of 15
13. What is the default layout manager for a JPanel?
a. FlowLayout
b. BorderLayout
c. GirdLayout
d. No default layout manager
Ans a
14.Suppose you want to display a text in multiple lines, which component should you use?
a. JButton
b. JLabel
c. JTextField
d. JTextArea
Ans d
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 9 of 15
Part IV: Comprehension of java programming code (Total 36pts. ):
Please indicate what is displayed on the console(standard output) when the following program/code is
executed? Please note that all the given codes can be compiled and executed.
1. (5 Points) Consider the following classes saved separately:
public class Base
{
private int num = 10;
public void print()
{
System.out.println(num);
}
public void setNum(int j)
{
num = j;
}
}
public class Derived extends Base
{
public void setNum(int j)
{
super.setNum(j + 10);
}
}
public class BaseDriver{
public static void main(String[] args)
{
Derived d = new Derived();
d.setNum(20);
d.print();
}
}
What is the output?
Answer
30
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 10 of 15
2. (5 points)Consider the following classes saved in separate files:
public class Base
{
public void print(Base b)
{
System.out.println("Base");
}
}
public class Derived extends Base
{
public void print(Derived b)
{
System.out.println("Derived");
}
}
public class BaseDriver{
public static void main(String[] args)
{
Base d1 = new Base();
Derived d2 = new Derived();
d1.print(d1);
d1.print(d2);
}
}
What is the output?
Answer
Base
Base
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 11 of 15
3. (6 points)Consider the code below:
void myMethod()
{ try
{
fragile();
}
catch( NullPointerException npex )
{
System.out.println( "NullPointerException thrown " );
}
catch( Exception ex )
{
System.out.println( "Exception thrown " );
}
finally
{
System.out.println( "Done with exceptions " );
}
System.out.println( "myMethod is done" );
}
What is printed to standard output if fragile() throws an Exception called
IllegalArgumentException?
Ans
Exception thrown
Done with exceptions
myMethod is done
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 12 of 15
4. ( 6 points)What is displayed on the console when the following program is run?
public class Test {
public static void main(String[] args) {
try {
int[] a = new int[10];
a[10] = 1;
System.out.println("Welcome to HTML");
}
catch (Exception ex) {
System.out.println("There is an exception”);
}
finally {
System.out.println("The finally clause is executed");
}
}
}
What is the output?
Answer
There is an exception
The finally clause is executed
5. (6 pts)Design a java interface called Priority that includes two methods: incrementPriority
(void method)and getPriority(returns an integer). The interface should define a way to
establish numeric priority among a set of objects.
Ans
interface Priority
{
public void incrementPriority();
//public int getPriority();
}
public class Test implements Priority
{
int priority;
public void incrementPriority()
{
priority++;
}
int getPriority()
{
return priority;
}
public static void main( String argv[] )
{
Test test=new Test();
test.incrementPriority();
System.out.println(test.getPriority());
}
}
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 13 of 15
6. ( 8 points)What is the output of the following program? Please view/use the “References”
section at the end of the exam.
import java.util.*;
public class Exercise{
public static void main(String[] arg){
ArrayList list = new ArrayList();
list.add(0,"One");
list.add(1,"Two");
list.add(2,"Three");
list.add(3,"Four");
list.add(4,"Five");
System.out.println(list);
list.remove( 3 );
System.out.println(list);
System.out.println(list.get(2));
list.set(1,"Hello");
System.out.println(list);
}
}
What is the output?
Ans
[One, Two, Three, Four, Five]
[One, Two, Three, Five]
Three
[One, Hello, Three, Five]
Reference: java.util
Class ArrayList
Resizable-array implementation of the List interface. Implements all optional list operations,
and permits all elements, including null. In addition to implementing the List interface, this
class provides methods to manipulate the size of the array that is used internally to store the
list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.
Method Summary
boolean add(E o)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean addAll(Collection ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- basic java programming examples
- java programming examples pdf
- java programming for beginners pdf
- learning java programming pdf
- basic java programming examples pdf
- java programming examples for beginners
- java programming questions for practice
- java programming tutorial for beginners
- java programming tutorial pdf
- java programming language book pdf
- java programming textbook pdf
- download free java programming book