0% found this document useful (0 votes)
4 views

Java Past Question 2

This document is an examination paper for a BSc in Information Technology, focusing on Advanced Java Technologies. It includes multiple-choice questions covering various Java concepts such as abstract classes, inheritance, polymorphism, and database connectivity. Additionally, it contains programming tasks requiring the implementation of Java classes and GUI applications.

Uploaded by

oforikelvin71
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)
4 views

Java Past Question 2

This document is an examination paper for a BSc in Information Technology, focusing on Advanced Java Technologies. It includes multiple-choice questions covering various Java concepts such as abstract classes, inheritance, polymorphism, and database connectivity. Additionally, it contains programming tasks requiring the implementation of Java classes and GUI applications.

Uploaded by

oforikelvin71
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/ 17

. ·-- - ... . . ..... .. . - .....

FACULTY OF INFORMATICS
BSc. INFORMA110N TECHNOLOGY (LEVEL 300)
END-OF-SEMESTER EXAMINATIONS, MAY 2019
ICT 314: ADVANCED JAV A TE CH NO LO GIE S

Duration: 2 ..00 hn
Answer all questions

Section A
1. A(n) _ class is a class that you create only to extend from.
A. abstract
B. inheritance
C. parent
D. final
2. You sometimes c.~eat.e an abs~~~~~ ~l~ss only so you can
_ it to anoth~~ class.
A. link
B. copy
C. extend
D. abstract '(::-i

~;~
3. A(n) _ is not an object, but it points to a memory address.
A. abstract class
B. reference
C. extend
D. interface

4.An application's ability to select the correct ~-ubclass method


is known as
A. implementing methods
B.

dynamic method binding
C. implicit casting
D. method fixing ·
5.Which of the following statements creates an f hr .
. O
A. Animal[) ref== new Animal( ); array t ee Animal references'?
3 ·
·- -- -- -- --
·-------~-----·- --
r~

B. Animal[] new= ref Animal[3];


C. Animal[] anim alRe f= new Animal[3];
·. your class is an
D. Animal[] ref= extend Animal[3]; . .. .
. . . ~ d anoth er class ,
------ ~-- • ·
6.Wh en you define a class , if you do not exphc 1tly ,.,xten
-- ------
·• ·

- ·_-.. _cla ss . ... ...---- - ----- ···--·- ·· ...


extension of the_
A. Object
B. super
C. publi c
. information
D. abstract · that cont ains
.
a Stnn g
7.The Obje ct class _ _ meth od conv erts an Obje ct mto
abou t the Object.
A. equa ls()
B. setTy pe()
C. toStr ing()
. . usually more usef ul
D. speak () 15
with your class es, it
8.lns tead of using the autom atic toStr ing() meth od the data
. 0 r all of
meth od that displ ays som e
to write your own _ _ versi on of the toStr ing() . . . -
.
field value s for the obje ctwi th-whiGh--y ou us0 -it.
• ---- - ·-· -- -·

A. over loade d

B. prote cted

C. static

D. over ridde n
_ _ valu e indic ating whether the objects are
9.Th e Obje ct class equa ls() meth od retur ns a(n)

equa l.
A. int

B. equa ls

C. null

D. bool ean
f
to __ class es: you would need to creata.\;, every part o a prog ram
1O.If Java did not allow you
from scrat ch.

A. extend ·----- .,

B. override
C. abstract
D. import

- ------- - - ... 2 7
. laY and
d to d1sP
·t can be use
1
17.The method is useful in debugging a program because
examine values.
A. finalizeO
B. toStringO
C. notl.fyO - -- ........ .
D. equals() . dered object-
18.lf a programming language does not support _, th e language is not cons1
oriented.
A. syntax
B. applets
C. loops
D. polymorph ism
19. When you create parent and child classes of your own, the child classes use -
constructor s.
A. two
B. As many
C. four
D. six
I
20. When you create a class and do not provide a(n) _ _ , Java automatica lly supplies you with
·1
a default one.
I A. constructo r
B. argument
C. header
I
I 1 D. name
I
I
21. Usually, the subclass constructo r only needs to initialize the that are ~ · fi th
-- vt'ec1 lC to e
subclass.
A. objects
B. data fields
C. methods
D. constructo rs
22.You can use the keyword _ _ _ within a method in a derived cl .
ass to acc~ss an
overridden method in a base class.
A. sub
B. this

---- ----- -
C. protected
D. super
23.When you employ __, your data can be altered only by the methods you choose and only
in ways that you can control.
A. virtual method calls
B. polymorphism
C. information hiding
D. inlining
24.Using the keyword __ provides you with an intermediate level of security between public
and private access.
A. protected
B. this
C. super
D. secure
25.The methods in a subclass can use all of the data fields and methods that belong to its
parent, with one exception: __ members of the parent class are not accessible within a child
class's methods.
A. private
B. public
C. final
D. protected
26. How can we connect to database in a web application?
A. oracle sql developer
B. toad
C. JDBC template
D. mysql
27. How can we take input text from user in HTML page?
A. input tag '··
B. inoutBufferedR.eader tag
C. meta tag
D. scanner tag
28. The Java API for developing java database applications is called
- -- - - --
A. JDBC
B. Servlet

... 5'7
C. JavaDB
D. Connection Manag
er
29. What is the di
fference between servlets an
i.Servlets execute d applets?
on Server; Applets
execute on browse
ii.Servlets have no r
GUI; Applet .b~.Q
... S \JJ .
m. erv lets creates . - .--
static we b pages; A 1
pp ets creates dynamic web pages .
iv.Servlets can hand
le only a single re
quest; Applet can ultiple· requests
hand le m
A. i,ii,iii are correct
B. i,ii are correct
C. i,iii are correct
D
.. 1,11,111,1v are correc
t
30 • \Vlnch of the fo
llowing is correct . A by class B?
way of implementin
g an mterface .
A. class B extends A
{}
B. class B implemen
ts A{}
C. class B imports A{}
D. None of the mentio
ned
\ Section B

- ,c ol <, r: S t.r
in g
-f ll le c. l: bo
ol ea n
.... _-do.t~C.re at ed
: ja vn . ut i 1
. Da1:e
~"'°"'"t ri cO bj ec ~ O
IC eo m et .ri cO
bj e-ct (. co lo r;
b l le d; bo ol st d n g .
ea n)
+g <e t;C ol Q• ·O
: St ,·i n~1
+s e~ C ol or (c
o1 or : s~ ri n
+ ;5 F ii le d () g) : vo id
: bo ol ea n :
~-! .e t'F i 1 l ed (.
fi 11 ed : bo ol
+g e~ D at •C .r ~a e, m '): vo id
~e ~( ): ja ya ." \
+1 .o St ri no O
: S tr in g
~ il .O at ~
+g et 'A re ~( ) :
do~P1~
+ ge t: P er im
et er () : do
ub le
j'.
··.-~·t~~·;~J:~1~~~-
~-~ ~~~~~&
- r- ad iu s: do ub9,i
-;(:-i- -;:~-,-; 6 -- . . -----
le
----- -- .....
+ C ~n:: 1e ( r al
l 'i us .: do ub ·1~
-4.( -r rc ht (r ad )
fi ll ed : bo iu s : do uh 1E
o1 ea n) · '.• .. ·
-g.::-rR,'\ ,'ii u, :.(
•o lo .- : st ri n
): do~h-7 e
g,
~-s et :R,ld ·i us {.
r·a di us .: do ~,
,;.q e't "D i amca't< b -1< !): v<.Ji'-i·
sr () ~ do ub 1 do ub 1~ ) l
e
ri ol tb \ e. \

\
\
SECTION B

Convert the UML class diagram above into a java code with conditions
Implement the abstract methods; getArea() and getPerimeter(). Create a
class called test, The main) method in the test class should create three
Circle objects and two Reciangle objects. The main) method should also
create indirectly an object of the Geometric class. Define a method in the
test class have two objects input parameter and checks whether they are
the same or not. Define a method in the test class that has one object input
parameter and prints its Area, Perimeter and Date it was created.
Demonstrate polymorphism in your test class.
Solution
import java.util.Date;
//Geometric Class is an abstract Class with two abstract methods
//That is, getArea and getPerimeter

public abstract class GeometricObject {


private String color;
private boolean filled;
private Date dateCreated;

protected GeometricObject(){

}
protected GeometricObject(String color,boolean filled){
this.color=color;
this.filled=filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled=filled;
}
public Date getDateCreated(){
dateCreated=new Date();
return dateCreated;
}
public String toString(){
return color+" "+filled;
}
public abstract double getArea();
public abstract double getPerimeter();

//The Circle Class


public class Circle extends GeometricObject {
//properties
double radius; //radius : double

//constructor 1
Circle(double radius){
super();
this.radius = radius; }

//constructor 2 with super class properties


Circle(double radius,String color,boolean filled){
super(color,filled);
this.radius = radius; }

//getters and setters methods for all properties

//radius
public double getRadius(){
return this.radius; }

public void setRadius(double radius){


this.radius=radius;
}

//diameter calculation
public double getDiameter(){
return 2*this.radius;
}

//we have to implement abstract methods


//area calculation
public double getArea(){
double area=Math.PI*this.radius*this.radius;
return area;
}
//perimeter calculation
public double getPerimeter(){
double perimeter=2*Math.PI*this.radius;
return perimeter;
}

//The Rectangle Class


public class Rectangle extends GeometricObject {
//Properties
private double width;
private double height;

//Constructor 1
public Rectangle(){
}
public Rectangle(double width, double height){
this.height=height;
this.width=width;
}
public Rectangle(double width, double height,String color,boolean filled
){
super(color, filled);
this.height=height;
this.width=width;
}
public double getWidth() {
return this.width;
}

public void setWidth(double width) {


this.width = width;
}

public double getHeight() {


return this.height;
}

public void setHeight(double height) {


this.height = height;
}

@Override
public double getArea() {
return this.width*this.height;
}

@Override
public double getPerimeter() {
return (2*this.width)+(2*this.height);
}
}
//The Test Class
//Test class having main method where polymorphism is implemented
public class test {
public static void main(String[] args) {
//Three Circle Objects
Circle c1=new Circle();
Circle c2=new Circle();
Circle c3=new Circle();
//Two Rectangle Objects
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();

//Indirect Object to Geometric class


GeometricObject geo=new Circle(7,"Green",true);

//Demonstrating Polymorphism
GeometricObject obj=new Circle();
GeometricObject ob2=new Rectangle();

//It means that, the super class reference variable has been assigned
//the subclass Circle and Rectangle
//Again we have typeCasted the superClass GeometricObject to a
//subclass Circle
if(obj instanceof Circle){
Circle obj1=(Circle)obj;
}

}
//Method that checks wether they are the same or not with two Object
//Parameters
public static boolean checkEquals(Circle c,Rectangle r){
boolean result;
if(c.equals(r))
result=true;
else
result=false;
return result;
}
//Method with one Object Parameter which returns Area,Perimeter
//and Date
public static void printDetails(GeometricObject obj){
System.out.println("Area: "+obj.getArea()+" \nPerimeter:
"+obj.getPerimeter()+" \nDate: "+obj.getDateCreated());
}
}
Question 2

Solution

Design and implement a GUI application that has a yellow circle in the
middle with two buttons Enlarge and Shrink at the bottom. When the
Enlarge button is clicked, the circle should increase in size by 2 pixels and
when the Shrink button is clicked the circle should reduce in size by 2
pixels The program output should be similar to the output below. e
ControlCircle Enlarge Shrink

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ControlCircle extends Application {


private CirclePane circlePane = new CirclePane();

@Override // Override the start method in the Application class


public void start(Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button("Enlarge");
Button btShrink = new Button("Shrink");
hBox.getChildren().addAll(btEnlarge,btShrink);
// Create and register the handler
EnlargeHandler handle = new EnlargeHandler();
btEnlarge.setOnAction(handle);

BorderPane borderPane = new BorderPane();


borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);

// Create a scene and place it in the stage


Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle("ControlCircle"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

public class EnlargeHandler implements EventHandler<ActionEvent> {


@Override // Override the handle method
public void handle(ActionEvent e) {

circlePane.enlarge();
}
}

/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}

class CirclePane extends StackPane {


private Circle circle = new Circle(50);

public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}

public void enlarge() {


circle.setRadius(circle.getRadius() + 2);
}

public void shrink() {


circle.setRadius(circle.getRadius() > 2 ?circle.getRadius() - 2 :
circle.getRadius());
}
}

You might also like