Skip to main content
留学咨询

辅导案例-COMPS311F

By May 15, 2020No Comments

COMPS311F – Online Exam (Sample) xx May 2020 Time Allowed: 2.5 hours (9:30 – 12:00) Instructions: – You should attempt ALL questions of the paper within the prescribed time. – The total mark is worth 100 marks. – Accept minor syntax errors. – No need to include import statements. – No need to handle exception except the questions are on exception or used as part of the program logic. – Submit a report containing answers to ALL the required questions. Your report must include all the Java programs. Your report should be stored in a Word format (.doc or .docx). Answers not included in your document file will not be marked. – Put your name and student number on every page of your report. – Name the document file in the format of COMPS311F-Exam-SXXXXXXX.doc (or .docx), where XXXXXXX is your ID. – Submit the document file to the “Take-home Exam” on the OLE (DO NOT put it in the “Discussion Board”). o o “Multiple submission” is NOT allowed. It means that you can only submit your answer file to the system once. o You are strongly advised to try out the Trial Take-home Examination to familiarize yourself with the submission process before the exam. – You should click the “Save & Submit” button after your document file has been uploaded. o – Without specification, the submitted version on the “Take-home Exam” of the OLE will be used for marking. – Zero mark will be given for those who complete the submission after the prescribed time. – Open book examination rules are applied. You are only allowed to use the internet to access the OLE and the relevant Java API. You will be disqualified if you access to any shared drives or social networking websites. Also, according to the regulation, cheating is prohibited, includes but is not limited to “aiding or attempting to aid another candidate, or obtaining or attempting to obtain aid from another candidate”. Page 2 of 9 Question 1 [5 marks] State whether each of the followings is true or false about Java: (a) A protected method in a class cannot be overridden in a subclass. (b) All variables are reference variables. (c) A private attribute cannot be accessed anywhere. (d) All methods must have at least one return statement. (e) When a thread has successfully invoked a synchronized method of an object, no other threads can invoke any of the methods of the object at the same time. [5] Question 2 [6 marks] Write a class for a simple game. The game starts with generating two random integers. Then the first who can give the sum of the two integers wins. The class looks like this: public class Game { ….. public Game() {…} syncrhonized public int[] getNumber() {…} synchronized public boolean answer(int i) {…} } The constructor is to generate two random integers between 0 and 100 inclusive. The getNumber method is for a player to get the two numbers. If someone has already given the correct answer, then null will be returned. The answer method is used by a player to give the answer. It will return true if no one else has provided the true answer and the answer provided by the player is correct. [6] Question 3 [5 marks] Write a program CountIntegers to read a number of files that contain only integers. The integers have been written to the files using writeInt of DataOutputStream. You need to use a separate thread to read in one file. Then, you need to write out the number of integers in each file in ascending order. For example, after executing the command: java CountIntegers file1 file2 file3 May generate the output: file3 has 256 integers file2 has 345 integers file1 has 1024 integers Note that you need a data structure to store the numbers of integers in the files. The thread which finished last is responsible for sorting and outputting the result. [5] Page 3 of 9 Question 4 [6 marks] (a) In a static scoping language, when a subprogram encounters a non-local variable, where should this be found? [1] (b) Consider the following Pascal code: program A; var a1:integer; procedure E; forward; procedure B; var b1:integer; procedure C; var c1: integer; begin {C begins} …… end; {C ends} procedure D; var d1:integer; begin {D begins} …… end; {D ends} begin {B begins} …… end; {B ends} procedure E; var e1:integer; begin {E begins} …… end; {E ends} begin {A begins} …… end. {A ends} Write the ARI records in the stack when the following procedure calls are made: – Program A calls procedure B – Procedure B calls procedure D – Procedure D calls procedure E – Procedure E calls procedure B [5] Question 5 [5 marks] Write a Java class which models some online resources. The resources can only be used by at most three users at a time. The resources are represented as the Java class Resource which has been defined elsewhere. Now, you need to implement the class that control the use of the resource: Page 4 of 9 public class ControlResources { private Resource resources[]=new Resource[3]; // the three instances of resource … synchronized public Resource get(boolean waitToGet) {…..} synchronized public void returnResource(Resource res) {….} } The get() is invoked by a user who wants to get a resource. It accepts a boolean parameter. If the parameter is true, then the user will wait if at the moment no resource is available. If the parameter is false, then the user does not want to wait. In this case, if there is no available resource, the method will just return null. The returnResource() method is used for a user to return a resource to the ControlResources object. You can assume that only those users who have got a resource from the object will call this method to return it. [5] Question 6 [5 marks] A database contains information regarding students and courses studied in a university. Student (Information of students) Attribute Data type Name varchar(50) StudentID varchar(10) Address varchar(100) Study (Study records of students) Attribute Data type StudentID varchar(10) CourseID varchar(10) Grade varchar(2) Course (Information of courses) Attribute Data type CourseID varchar(10) Title varchar(50) Complete the following JSP that displays the student who failed a particular course (Grade F). The id of the course should have been passed as the parameter named courseid. Class.forName(“com.mysql.jdbc.Driver”); String url = “jdbc:mysql://192.168.62.100:3306/2014exam”; Connection con = DriverManager.getConnection(url, “comps311f3”, “comps311f3”); %> Course records Page 5 of 9 // Add code here %> The following example shows the content when the request URL is: http://plbpc001.ouhk.edu.hk/jsp/get.jsp?courseid=comps311 Only 3 students have failed this course. The title of the course is “Java Application Development”. [5] Question 7 [5 marks] (a) List one advantage and one disadvantage of passing parameters by value. [2] (b) Consider the following C like code: #include int a=3; int b=4; void fun(int x, int y) { a=2*x; x=y/2; y=a; } void main() { fun(b,b); printf(“%d %d\n”,a,b); } What is the output of the program if the parameter passing method is: (i) Pass-by-value. (ii) Pass-by-value-result. (iii) Pass by reference. [3] Page 6 of 9 Question 8 [6 marks] Consider the following xml file: The Big Love David Au Park Kiu Calculus Ma Ko In each , there are a number of s. can be empty. In , there must be one and at least one . Write down the XML Schema for this XML file. [6] Question 9 [6 marks] (a) Describe the difference between the reserved words “throw” and “throws” in Java. [2] (b) Rewrite the following method without using any boolean expression. (Hint: use try-catch blocks to handle any exception.) static public int set(int a[], int i) { if (i>=0 && i a[i]=i*2; return a[i]; } else { return 0; } } [4] Question 10 [6 marks] State the problem in each of the following Java code fragments. (a) … switch (a) { case 0: i=4; case 1: i=5; Page 7 of 9 } … (b) … int a[]=new int[10]; for (int i=1;i<=10;i+ +) { a[i]=...; ... } ... (c) public class ABC extends DEF1, DEF2 { int a=3; public ABC() {...} } (d) public class ABC extends DEF1 { int a=3; public DEF1() {...} } (e) abstract public class ABC { int a=3; abstract public ABC(); ... } (f) abstract public class ABC { abstract int a; public ABC() {...} } [6] Question 11 [15 marks] (a) Consider the following code that has syntax similar to C: int a[5][4][4][4]; a[3][3][1][3]=3; a[1][2][3][1]=4; a[4][0][1][1]=5; a[2][3][2][3]=6; Given that an int has a size of 4 bytes. Assume that a[0][0][0][0] is at the address of 2000. Find the address of (i) a[3][3][1][3] if row major is used. (ii) a[1][2][3][1] if row major is used. (iii) a[4][0][1][1] if column major is used. (iv) a[1][2][1][2] if column major is used. Page 8 of 9 [4] (b) Write a multithreaded Java server which is used to handle public voting: - It listens to requests at port 12345. - The system allows 100 users to cast a vote on an issue. - When a client request arrives at the port, a new thread is created to serve it. - The server then send a string using the writeUTF method of DataOutputStream to the client depending on the number of received votes: o If already 100 votes have been received, then the string to be sent to the client is "The vote has ended, xx people say yes, yy people say no." Then the connection will be closed. xx and yy are the number of yes votes and no votes respectively. o Otherwise, the string to be sent is: "Please cast your vote which should be either yes or no." Then the client should either send in "yes" or "no" using the writeUTF method. The server should then register the vote and close the connection. You can assume that all users behave normally and do not need to handle exceptional cases. [Hint, you should make sure your server is thread safe.] [11] Question 12 [15 marks] (a) Compare the readability, writability and reliability of two languages A and B in the following aspects: (i) In A, there is no need to declare a variable before using it. In B, a variable must be declared before using it. (ii) In A, the loop counter in a for loop can be assigned a different value anywhere in the loop. In B, the loop counter can only be changed in the predefined way after each iteration. [6] (b) Consider the following Java code: public class ABC { private static int a=4; private int b=5; public int meth() { return a+b; } } Write down whether each of the followings is done during language design time, language implementation time, compile time, load time or run time. (i) The type of the identifier ABC is fixed. (ii) The address of a is fixed. (iii) The address of b is fixed. (iv) The address of meth is fixed. (v) The meaning of the keyword class is fixed. Page 9 of 9 [5] (c) Consider the following C like code fragment: int x=3; int y=2; void fun() { int x=y*2; y=4-x; printf("fun:%d %d\n",x,y); } void main() { int y=x-3; x=2+y; fun(); printf("main:%d %d\n",x,y); } Write down the output of the program if the language uses: (i) Static scoping. (ii) Dynamic scoping. [4] Question 13 [15 marks] (a) (i) Explain why the public key method is never used to encrypt the contents of a message. (ii) If you want to take advantage of the public key method but want to protect the confidentiality of a message, what can you do? (iii) Explain why message digest alone cannot be used to protect the integrity of a message. What other tools are needed to fulfill this purpose? [6] (b) Write the Java application CreateDigest so that when the following command is executed: - java CreateDigest input.dat output.dat The message digest of the file input.dat will be generated using the SHA-1 algorithm. The message digest will be stored at the file output.dat. The file names should be read from the arguments instead of hardcoded in the program. [9] Please check that you have written your student name and student number on every page of your report. Failure to do so will mean that your work cannot be identified. [END OF TAKE-HOME EXAM (SAMPLE) PAPER]

admin

Author admin

More posts by admin