Advanced Sample Exam

You might also like

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

Arab Academy for Science, Technology and Acad.

Year : Fall 2020/2021


Maritime Transport-College of Computing & Semester : 4
Information Technology--Heliopolis Department : Computer Science
No. of Pages : Two Time Allowed : 120 Minutes
No. Of Questions : Four Date : 1 / 2 / 2021
Examiner : Dr. Mohamad Hashim
CS244 Advanced Programming Applications (Final Exam) (40 Points)

Question One (10 Points)

Complete the following statements:


i) html is a language consisting of tags to generate static web pages, while jsp also contains
tags but is used to generate dynamic web pages.
ii) In JSP, jsp:include directive is used to include static resources at the translation time, but
jsp:forward is used to include resources at the request time, so it is more suitable for
including dynamic resources.
iii) To use a database inside a Java program, we must follow these basic steps: Establish a connection
,Create JDBC Statements, Execute SQL Statements, GET ResultSet and Close connections .
iv) In JDBC, when executing parameterized queries, it is preferred to use PreparedStatement
Because reduces parsing time as the preparation on the query is done only once and Bound
parameters minimize bandwidth to the server .
v) To define a thread in Java, we define an object that extends thread class or we define an object
that implements runnable interface.
vi) The start method in the Thread class starts a thread, while Thread.sleep (100) will make the
current thread sleep for _ 0.1 seconds and the Thread priority can be changed by using
setpriority method.
vii) A java class to be a Bean must satisfy the following properties: public class , have a no-arg
constructor, be Serializable and getter and setter methods.
Question Two (10 Points)

a) Write an HTTPServlet class equivalent to the following JSP page:

<% String name = request.getParameter(“uname”); %>


<%! int n = 0; %>
This page is accessed: <%= ++n %> times from the user <%=name %>

private int n;
public void init() {
n = 0;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
String name = request.getParameter(“uname”);
n++;
PrintWriter out = response.getWriter();
out.print(‘this page is accessed ’+ n +’times from the user’+name);
}

CS244 Advanced Programming Applications Page 1/2 MPC6/1-1


b) Write a Java program that launches 1,000 threads. Each thread adds 10 to a variable sum that
initially equals 0. When all the threads have finished, print the final value of sum (should by
10,000). Write this program once using Thread class and another using the ExecutorService
interface.
public class SumThreads extends Thread{

int start, end, sum=0;

int []a;

public SumThreads(int []a, int start, int end){

this.start = start;

this.end = end;

this.a = a;

public void run(){

for(int i=start; i<end; i++){

sum += a[i];

}}

public static void main(String[] args) throws InterruptedException {

int numSize = 10000;

int []a = new int[numSize];

for(int i=0; i<numSize; i++){

a[i] = 1;

int numThreads = 1000;

SumThreads[] threads = new SumThreads[numThreads];

int chunck = numSize / numThreads;

int start, end;

for(int i=0; i<numThreads; i++){

start = i * chunck;

end = start + chunck;

threads [i] = new SumThreads(a,start,end);

threads[i].start();

int totalSum=0;

for(int i=0; i<numThreads; i++){

threads[i].join();

totalSum += threads[i].sum;

} System.out.println(totalSum); }}

CS244 Advanced Programming Applications Page 2/2 MPC6/1-1


Question Three (10 Points)

a) Differentiate between the following:


i) The GenericServlet class and the HTTPServlet class.
ii) The forward () method (of the RequestDispatcher interface) and the sendRedirect ()
method (of the HttpServletResponse interface)

GenericServlet HTTPServlet
 class implements Servlet, ServletConfig and  extends the GenericServlet class and
Serializable interfaces. implements Serializable interface
 provides the implementation of all the  part of the javax.servlet.http package
methods of these interfaces except the service  It provides http specific methods such as
() method doGet, doPost
 can handle any type of request so it is  functionality of this class makes it easier to
protocol-independent. build servlets that work with HTTP requests
 create a generic servlet by inheriting the and responses
GenericServlet class and providing the
implementation of the service method.

forward () method (of the RequestDispatcher the sendRedirect () method (of the
interface) HttpServletResponse interface)

 Forwards a request from a servlet to another  interface can be used to redirect response to
resource (servlet, JSP file, or HTML file) on another resource, it may be servlet, jsp or
the server. html file.
 interface provides the facility of sending the  works at client side because it uses the URL
request to another resource bar of the browser to make another request

CS244 Advanced Programming Applications Page 3/2 MPC6/1-1


b) Find and correct (at least 10 errors) in the following code fragments:

a) b)
1. class MyThread implements Runnable 1. class ABC extends JDBC
2. { char ch; int n; 2. { public static void main (String[ ] args)
3. MyThread (char c, int n) 3. { Class.forName ("com.mysql.jdbc.Driver");
4. { ch = c ; n = n; } 4. Connection con=DriverManager.
5. public void start( ) getConnection ("jdbc:mysql:///test", "root",
6. { for (int i=0 ; i<n ; i++) "secret");
7. System.out.print(ch); 5. Statement stm = con.createStatement( );
8. } } 6. String s = “select * from Student”;
9. class Program 7. ResultSet rs = stm.updateQuery(“s”);
10. { public static void main (String[ ] a) 8. for (int i=0 ; i<rs.length ; i++)
11. { Thread t = new Thread (MyThread); 9. {System.out.print (“ID:” + rs.getInt(0));
12. T.start (‘z’, 50 ); 10. System.out.println (“name:” + rs.getInt(1));}
13. T.start (‘a’, 10); 11. s = “select * from Student where acYear>1”;
14. } 12. int n = stm.updateQuery(“s”);
15. } 13. System.out.println(“You inserted n new
Students”);
14. }

Question Four (10 Points)

Given the below page (index.html) and a database table (StudentInfo),

Name: “id”

Name: “name”

Name: “acYear”

Name: “gpa”

Page: index.html Table: StudentInfo


Develop the following:

 A Java Bean called Student containing the data members: {id, name, acYear, gpa}.
 A JSP page called (process.jsp) that is called when clicking on the submit button at
index.html. This JSP page should do the following:
o If the user entered all data in the form:
 Create an object of Student initialized by the data entered at the page index.html.
 Insert the data of the created object into the table StudentInfo.
 Return back to the index.html page.
o If the user left the ID input text empty:
 Return back to the index.html page with an error message.
o If the user entered the value 99999 at the ID input text:
 All the data of the students whose GPA> = 3.0 are displayed in a new JSP page called
result.jsp.

Best Wishes

CS244 Advanced Programming Applications Page 4/2 MPC6/1-1

You might also like