Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Ministry of Higher Education Academic Year: 2019-2020

Modern Academy For Computer Subject: AdvancedProgramming Languages


Science and Management Technology Code: IT202
Department: Computer Science Specialization: CS – 2nd Level
Program: Computer Science Dr.\Lamiaa Hassaan

Advanced Programming Languages – CS 2nd Level


Sheet 1

1- Define the concept of “exception”

2- What are the ways to handle exceptions?

3- Differentiate between throw and throws keywords. Give examples.

4- Choose the correct answer:

I- What is the output of the following code:


II- class Main { public static void main(String args[]) {
III- int x = 0;
IV- int y = 10;
V- int z = y/x; } }

a- Compiles fine but throws an exception on the run time b- Compiler Error c-Compiles & runs fine

II- What is the output of the following code:


VI- public class App {
VII- String str = "a";
void A()
{
try{ str +="b"; B(); } a- abcd
catch (Exception e){ str += "c"; } b- aabc
} c- abfc
void B() throws Exception d- abdf
{
try{ str += "d"; }
catch(Exception e) { throw new Exception(); }
str += "f";
}
public static void main(String[] args)
{ App object = new App();
object.A();
System.out.println( object.str );}}
Ministry of Higher Education Academic Year: 2019-2020
Modern Academy For Computer Subject: Advanced Programming Languages
Science and Management Technology Code: IT202
Department: Computer Science Specialization: CS – 2nd Level
Program: Computer Science Dr.\Lamiaa Hassaan

Advanced Programming Languages – CS 2nd Level


Answers of Sheet 1

1- Define the concept of “exception”


An exception is a problem (error) that occurs during program execution. Exceptions can occur in
many cases, for example: User entered incorrect data, the file accessed by the program was not
found or Network connection to the server was lost during data transfer. Etc.

All exceptions in Java are objects. Therefore, they can be generated not only automatically in the
event of an exceptional situation, but also created by the developer.

2- What are the ways to handle exceptions?


try – this keyword is used to mark the beginning of a block of code that could potentially lead to an
error.

catch is a keyword to mark the beginning of a block of code intended to catch and handle exceptions.

throw – is used to generate exceptions.

throws is a keyword that is written in the method signature, and means that the method can potentially
throw an exception with the specified type.

The general view of the construction for “catching” an exceptional situation is as follows:

try{
//here is the code that could potentially lead to an error }

catch(SomeException e ){ //the class indicates the specific expected error

//This section describes actions to handle exceptions. }


3- Differentiate between throw and throws keywords. Give examples.
throws throw
Throws clause is used to declare an
throw keyword is used to throw an
exception, which means it works similar to the
exception explicitly.
try-catch block.
throw is followed by an instance of
throws is followed by exception class names.
Exception class
throws is used in method signature to declare
throw keyword is used in the method
the exceptions that can occur in the
body to throw an exception,
statements present in the method
you can handle multiple exceptions by
You can throw one exception at a time
declaring them using throws keyword
public class Example1{
public class Example1{
int division(int a, int b) throws
void checkAge(int age){
ArithmeticException{
if(age<18)
int t = a/b;
throw new ArithmeticException("Not
return t;
Eligible for voting");
}
else
public static void main(String args[]){
System.out.println("Eligible for
Example1 obj = new Example1();
voting");
try{
}
System.out.println(obj.division(15,0));
public static void main(String args[]){
}
Example1 obj = new Example1();
catch(ArithmeticException e){
obj.checkAge(13);
System.out.println("You shouldn't divide
System.out.println("End Of
number by zero");
Program");
}
}
}
}
}

4- Question I: Answer is a Question II: Answer is d


Ministry of Higher Education Academic Year: 2019-2020
Modern Academy For Computer Subject: Advanced Programming Languages
Science and Management Technology Code: IT202
Department: Computer Science Specialization: CS – 2nd Level
Program: Computer Science Dr.\Lamiaa Hassaan

Advanced Programming Languages – CS 2nd Level


Sheet 2

1- What is an applet? What are the advantages of an applet?

2- What are the methods within a lifecycle of an applet?

3- What is the output of the following code:


import java.awt.*;
import java.applet.*; a) 20
public class myapplet extends Applet b) Default value
{ c) Compilation Error
Graphic g; d) Runtime Error
g.drawString("A Simple Applet", 20, 20);
}

4- Which is a drawback for Applet?


a. Takes a long time to run b. Secured c. Plugin is required at client browser to execute applet

5- Which is the correct order of lifecycle in an applet?


a. Applet is started,initialized,painted,destroyed,stopped

b. Applet is initialized,started,painted,stopped,destroyed

c. Applet is painted,started,stopped,initilaized,destroyed

d. None of the above


Ministry of Higher Education Academic Year: 2019-2020
Modern Academy For Computer Subject: Advanced Programming Languages
Science and Management Technology Code: IT202
Department: Computer Science Specialization: CS – 2nd Level
Program: Computer Science Dr.\Lamiaa Hassaan

Advanced Programming Languages – CS 2nd Level


Answers of Sheet 2

1- What is an applet? What are the advantages of an applet?

 An Applet is a special type of program that is embedded in the webpage to generate the dynamic content.
 An applet is part of a web page, just like an image or hyperlink.
 It runs inside the browser and works at client side.

Advantages of an applet:

 It works at client side so less response time.


 Secured
 It can be executed by browsers running under many platforms, including Linux, Windows, Mac OS etc.

2- What are the methods within a lifecycle of an applet?

 Initialization: public void init(): is used to initialized the Applet. It is invoked only once.
 Start: public void start(): is invoked after the init() method or browser is maximized. It is used to start the
Applet.
 Paint: public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can
be used for drawing oval, rectangle, arc etc.
 Stop: public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is
minimized.
 Destroy: public void destroy(): is used to destroy the Applet. It is invoked only once.

3- Answer: c
Explanation: To implement the method drawString we need first need to define abstract method of AWT
that is paint() method. Without paint() method we can not define and use drawString or any Graphic class
methods.

4- Answer: c

5- Answer: b

You might also like