联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2019-01-21 10:18

EBU6042 Paper A ‐ SOLUTIONS 2016/17

Question 1

a) The questions below refer to JavaScript and the DOM:

[9 marks]

i) Indicate what the DOM abbreviation stands for and name THREE examples of objects found in

the DOM.

(4 marks)

ii) Consider the JavaScript code fragment in Figure 1 and fill in the THREE gaps in the sentences

below; you are only required to write down what the blanks marked 1 to 3 correspond to.

Figure 1

With regards to the code in Figure 1, if the user enters a response and clicks the OK button, then

the value of num will be _____1_____. If instead the user enters a response and clicks the Cancel

button, the value of num will be _____2_____. If the user clicks the OK button without entering

a response, then the value of num will be _____3_____.

(3 marks)

iii) Briefly describe the concept of event handler and indicate a typical use of onSubmit in a

webpage.

(2 marks)

Do not write in

this column

i) 4/9

DOM = Document Object Model [1 mark]

THREE examples of objects in the DOM: [3*1 mark: 3 correctly identified objects]

window, location, checkbox, submit, reset, button, radio, text

ii) 3/9

1. whatever value the user entered [1 mark]

2. Null [1 mark]

3. an empty string [1 mark]

iii) 2/9

Event handler: this is a command that JavaScript uses to deal with actions performed

by the user while visiting a webpage. [1 mark]

onSubmit does form validation using JavaScript, when the user submits an HTML

form. OR onSubmit triggers a function to run when a form is submitted. [1 mark]

9

marks


num = prompt("Please enter a positive number: ", "");

EBU6042 Paper A‐ SOLUTIONS 2016/17

b) Consider the HTML code shown in Figure 2 and answer the questions below:

[16 marks]

i) Assume that the code in Figure 2 is stored in file riverBusPlanner_home.html. Sketch how

the contents of this file would be displayed by a web browser. The webpage is for a company that

runs a river bus service, which allows people to travel by boat along the River Thames in London.

(8 marks)

ii) Write the code for the JavaScript function processInit() that is activated by clicking on the

Planner button, such that the following is checked:

1. That the user has filled in the email address and that it contains a ‘@’ sign in a position other

than at the start; if that field is empty or does not include a ‘@’ sign, then an alert box should

be generated with an appropriate message.

2. That the start and end river bus stops are different; if they are the same, an alert box should

be generated with an appropriate message.

If the above two checks are satisfied, then the webpage riverBusPlanner_journeys.html

is loaded. Note: It is not necessary to know the contents of this second HTML page.

Your answer must also state where you would need to add this JavaScript function, in the HTML

code describing the webpage in Figure 2.

(8 marks)

Figure 2

<HTML>

<HEAD> <TITLE>Thames River Bus</TITLE> </HEAD>

<BODY>

<FORM NAME="formInit">

<H3>Thames River Bus Service: Journey Planner</H3>

Email address: <INPUT TYPE="text" NAME="emailAddress">

<P><B>Time of day:</B>

<INPUT TYPE="radio" NAME="time" VALUE="peak">Peak time

<INPUT TYPE="radio" NAME="time" VALUE="offpeak">Off-peak time

<P><B>Choose a river bus stop for the start and end of your journey:</B></P>

Start&nbsp;

<SELECT NAME="start">

<OPTION SELECTED>Canary Wharf</OPTION>

<OPTION>Greenland</OPTION>

<OPTION>Masthouse Terrace</OPTION>

</SELECT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

End&nbsp;

<SELECT NAME="end">

<OPTION SELECTED>Masthouse Terrace</OPTION>

<OPTION>Greenland</OPTION>

<OPTION>Canary Wharf</OPTION>

</SELECT>

<P><B>Press Planner to look at available route options:</B>

<P><INPUT TYPE="button" NAME="plan" VALUE="Planner" onClick="processInit()">

</FORM>

</BODY>

</HTML>

Do not write in

this column

i) 8/16

[1 mark]: page title and header are both shown;

[1 mark]: email address textbox is shown, preceded by the appropriate label;

[2 marks]: 2 radio buttons on the same line, each followed by the appropriate label;

[2 marks]: 2 menus shown on the same line, each preceded by the correct label,

and each with the appropriate default option shown;

[1 mark]: Planner button is shown;

[1 mark]: the sketched page has a sensible overall layout.

ii) 8/16

function processInit() {

firstForm = document.forms["formInit"];

if (formInit.emailAddress.value == "")

alert("Please fill in your email address!");

else if (formInit.emailAddress.value.indexOf('@')< 1)

alert("Invalid email address!");

else if (formInit.start.value == formInit.end.value)

alert("Your origin and destination stops must be different!");

else window.location.href="riverBusPlanner_journeys.html";

}

EBU6042?Paper?A?‐?SOLUTIONS 2016/17

Page?4?of?17

Do not write in

this column

[1 mark: correct function declaration]

[2 marks: 4 checking conditions ? 1) start-end check, 2) empty email, 3) if @ sign

exists, 4) @ sign not first character; the last two could be 1 or 2 conditions]

[3*1 marks: 3 alert boxes with sensible messages]

[1 mark: an attempt at redirection]

The function processInit() should be placed in the <HEAD> section of the HTML

page [1 mark].

16

marks

Question marking: 9 16 25

Question 2

a) Consider the statements below about JSPs, servlets and sessions. Identify the statements that are

FALSE and correct them.

[7 marks]

1. A session object is created the first time that a browser requests a servlet or JSP from a website.

2. The current session object is available only to servlets.

3. A servlet communicates with the servlet engine in the web server via a request and response

object of the HttpServletRequest and HttpServletResponse classes respectively, in the

form of streams.

4. JSPs use delimiters such as <% and %> to include Java code that the JSP engine in the web server

then converts directly to HTML code.

5. In the Tomcat web server, the web.xml file has the tag <servlet-engine> that identifies the

name and the type of the servlet engine used by the web server.

Do not write in

this column

FALSE statements: 2, 4, 5 [3*1 mark]

Correction of FALSE statements:

2. The current session object is available to both servlets and JSPs. [1 mark]

4. JSPs use delimiters such as <% and %> to include Java code that the JSP engine in

the web server then converts to a servlet. [1 mark]

5. In the Tomcat web server, the web.xml file has the tag <servlet-name> that

identifies the class name of the serlvet or its alias name. [2 marks]

7

marks

b) Consider the webpage shown in Figure 3 and answer the questions below:

[15 marks]

i) Assume that a user’s personal data is sent to a JSP called StoreData.jsp, once the Submit

button is clicked. Write the code for a JavaBean called UserData that will store the user’s details

on the server side; you can assume that the JavaBean is stored in a package called user, and that

the user data parameters are called name and age.

(10 marks)

ii) Write the code for the JSP called StoreData.jsp that uses the JavaBean you wrote in part i)

to collect the user’s data. This JSP then loads another JSP called DisplayData.jsp, upon

clicking Continue.

Note: You are not required to write the code for DisplayData.jsp.

(5 marks)

Figure 3

Do not write in

this column

i) 10/15

package user; [1 mark]

import java.io.*;

public class UserData implements Serializable { [2 marks]

private String name; [1 mark]

private int age; [1 mark]

public UserData() { } [1 mark]

public void setName(String value) { name = value; } [1 mark]

public void setAge(int value) { age = value; } [1 mark]

public String getName() { return name; } [1 mark]

public int getAge() { return age; } [1 mark]

}


Do not write in

this column

ii) 5/15

<jsp:useBean id="usr" class="user.UserData" scope="session"/>

[2 marks]

<jsp:setProperty name="usr" property="*"/> [1 mark]

<HTML>

<BODY>

<A HREF="DisplayData.jsp">Continue</A> [1 mark]

</BODY>

</HTML>

[1 mark: sensible overall structure]

15

marks

c) Servlets have THREE lifecycle methods.

[3 marks]

i) Describe what the init() method does and when it is used.

(2 marks)

ii) Name the other TWO servlet lifecycle methods.

(1 mark)

Do not write in

this column

i) 2/3

The init() method is used by the container to initialise the servlet; it is invoked

only once in the lifecycle of the servlet. [2 marks]

ii) 1/3

Other TWO servlet lifecycle methods: service() and destroy() [2*0.5 mark]

3

marks

Question marking: 7 15 3 25

Question 3

a) The questions below refer to threads:

[12 marks]

i) What is a daemon thread? Which method in Thread is used to make a thread a daemon?

(2 marks)

ii) Write the code for a thread called PrintThread that accepts a String as a parameter in its

constructor. When running, PrintThread should print its String to the screen.

(4 marks)

iii) Write a main() method that constructs 100 new instances of PrintThread and then starts them

executing concurrently.

(4 marks)

iv) Will the output of main() be deterministic, i.e. will the console output always be the same?

Justify your answer.

(2 marks)

Do not write in

this column

i) 2/12

A daemon thread is a thread that is automatically terminated when its parent dies.

[1 mark]

The method is setDaemon(true). [1 mark]

ii) Note: The student can implement the thread by either extending the Thread class 4/12

OR by implementing Runnable.

public class PrintThread extends Thread { [1 mark]

private String toPrint = null;

public PrintThread(String toPrint) { [1 mark]

this.toPrint=toPrint;

}

public void run() { [1 mark]

System.out.println(toPrint); [1 mark]

}

} // End of class

Do not write in

this column

iii) 4/12

public static void main(String[] args) { [1 mark]

for (int i=0; i<10; i++) { [1 mark]

PrintThread thread = new PrintThread("Thread-"+i); [1 mark]

thread.start(); [1 mark]

OR (alternative approach – also worth 2 marks)

Note: Please ensure the approach matches the student’s answer to part ii).

MyRunnable runnable = new MyRunnable("Thread-" + i);

Thread thread = new Thread(runnable);

thread.start();

}

}

iv) 2/12

The output will be non-deterministic. [1 mark]

The order is based on when the threads get scheduled, which may change each time

the code is executed. [1 mark]

12

marks

b) The code in Figure 4 is a simplified form of a read-write lock.

[9 marks]

Figure 4

i) Explain the difference between a simple lock and a read-write lock. Why might a read-write lock

achieve better performance?

(2 marks)

ii) Explain the use of wait() in general, and its use in method lockWrite() of Figure 4 in

particular.

(4 marks)

iii) Explain the use of notify() and notifyAll() in general. Also explain how notifyAll()

is used in method unlockRead() of Figure 4 in particular.

(3 marks)

Do not write in

this column

i) 2/9

A read-write lock does not allow multiple threads to read or write data at the same

time. [1 mark]

However, concurrent reads are usually acceptable, so this is not a good idea. [1 mark]

public class ReadWriteLock {

private int readers = 0;

private int writers = 0;

private int writeRequests = 0;

public synchronized void lockRead() throws InterruptedException {

while(writers > 0 || writeRequests > 0) { wait(); }

readers++;

}

public synchronized void unlockRead() {

readers--;

notifyAll();

}

public synchronized void lockWrite() throws InterruptedException {

writeRequests++;

while(readers > 0 || writers > 0) { wait(); }

writeRequests--;

writers++;

}

public synchronized void unlockWrite() throws InterruptedException {

writers--;

notifyAll();

}

}

EBU6042?Paper?A?‐?SOLUTIONS 2016/17

Page?11?of?17

Do not write in

this column

ii) 4/9

wait() tells the calling thread to give up the monitor and go to sleep until some

other thread enters the same monitor and calls notify(). [2 marks]

If there are any readers or writers wanting access to the lock, the thread calling this

method (which wants to do a write) is put in the “wait set”. [2 marks]

iii) 3/9

notify() wakes up an arbitrary thread that called wait() on the same object.

[1 mark]

notifyAll() wakes up all the threads that have called wait() in no particular

order, so that they can try to acquire the monitor. [1 mark]

In unlockRead() there is one less thread wanting read access and notifyAll()

tells all the threads in the “wait set”; these could be threads wanting to do reads or

writes. [1 mark]

9

marks

c) The questions below refer to sockets and threads:

[4 marks]

i) Write the Java code to create a socket that connects to a server with the IP address 10.0.0.1. It

should connect to the server on port 80.

(2 marks)

ii) Write the Java code to create a server-side listening socket. It should listen on port 80.

(1 mark)

iii) Why is it important for a web server to be built using multiple threads?

(1 mark)

Do not write in

this column

i) 2/4

Socket s = new Socket("10.0.0.1", 80);

[1 mark: IP and port; 1 mark: socket]

ii) 1/4

ServerSocket ss = new ServerSocket(80); [1 mark]

Do not write in

this column

iii) 1/4

A web server must deal with multiple concurrent users. Thus, each user must be

served in its own thread. [1 mark]

4

marks

Question marking: 12 9 4 25

Question 4

a) Write a simple webpage that looks like the one in Figure 5. Ensure that you put all HTML elements

in, so that it could be correctly loaded by a web browser.

[4 marks]

Figure 5

Do not write in

this column

Note: Marks are allocated for opening and closing the HTML tags correctly.

If a tag is opened correctly, but not closed correctly, please allocate half marks.

<html> [0.5 mark]

<body> [0.5 mark]

<p>Here is a list</p> [1 mark]

Note: Other tags are allowed, e.g. <big>, <em>, <i>.

<ul> [1 mark]

<li>Bread</li> [1 mark]

<li>Crisps</li>

<li>Meat</li>

</ul>

</body>

</html>

4

marks

b) The following questions relate to Java’s Remote Method Invocation (RMI).

[16 marks]

i) Serializable is a marker interface. What is a marker interface? What does it mean when you

write a class that implements Serializable?

(2 marks)

ii) Explain the concept of skeletons and stubs in RMI. Include the following in your answer:

? Does each one operate on the client or on the server?

? What are the steps they take when a Remote Method Invocation is executed?

(8 marks)

Here is a list:

Bread

Crisps

Meat

iii) Write a remote interface for a server that adds two numbers together. The remote interface should

contain a single method call add(). The interface should be called AddServerInterface.

(4 marks)

iv) Describe what the client and server objects use the rmiregistry for in RMI. Note: You do not

need to write code.

(2 marks)

Do not write in

this column

i) 2/16

A marker interface is one that does not contain any methods; it just signals

something. [1 mark]

Implementing Serializable indicates that a class can be serialized by Java.

[1 mark]

ii) 8/16

SKELETON

A class that sits on the server side. [1 mark]

Receives remote method calls and parameters. [1 mark]

Invokes the method locally. [1 mark]

Sends the return value back to the client. [1 mark]

STUB

A class that sits on the client side. [1 mark]

Presents the same remote interface as the server object. [1 mark]

Accepts any method invocations and forwards them to the server. [1 mark]

Receives any return results. [1 mark]

iii) 4/16

public interface AddServerInterface [1 mark]

extends Remote { [1 mark]

public int add(int a, int b) [1 mark]

throws RemoteException; [1 mark]

}


Do not write in

this column

iv) 2/16

The rmiregistry is used by servers to register remote objects. [1 mark]

The rmiregistry is used by clients to lookup remote objects. [1 mark]

16

marks

c) The following questions are based on the HTTP response in Figure 6.

[5 marks]

Figure 6

i) What is the purpose of the status code in an HTTP response? What is the status code of the

response shown in Figure 6?

(2 marks)

ii) What type of content is in the payload of the HTTP response in Figure 6?

(1 mark)

iii) Will a browser cache this HTTP response? Explain your answer.

(2 marks)

Do not write in

this column

i) 2/5

The status code tells the client what is the status of the message. [1 mark]

The status code in the response is 200. [1 mark]

ii) 1/5

The content in this response is HTML. [1 mark]

HTTP/1.1 200 OK

Transfer-Encoding: chunked

Date: Sat, 13 Mar 2016 11:37:51 GMT

Server: Apache

Connection: close

Cache-Control: no-cache

Content-Type: text/html; charset=UTF-8

Last-Modified: Sat, 15 Jan 2016 09:50:17 GMT

Content-Encoding: gzip

Do not write in

this column

iii) 2/5

The browser will not cache the content [1 mark], because the Cache-Control

header is set to no-cache [1 mark].

5

marks

Question marking: 4 16 5 25


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp