ProgUseJava SampleExam-----PTJ_(01-08)_(reference answer参考答案)

 

INTERNATIONAL DIPLOMA IN COMPUTER STUDIES

                                   

PROGRAMMING TECHNIQUES USING JAVA

 

 

 

Registration No:

 

 

 

 

 

 

 

-

 

 

 

 

 

 

 

-

 

 

 

 

 

 

   Student No:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                         DURATION: 3 Hours and 10 Minutes Reading Time

 

 

INSTRUCTIONS:

 

ü     Students are reminded to write their own Registration and Student Number clearly on Question Paper and All Pages of Answer Scripts.

 

ü          Students are reminded to read the questions carefully.

 

ü          No reference material of any kind may be taken in the examination.

 

ü     Students must attempt ALL questions.

           

ü          There are 4 pages altogether.

 

 

 

QUESTION NO

MAXIMUM MARKS

MARKS OBTAINED

Part – I ( 1 to 10 )

10 x 2 = 20

 

Part – II ( 1 to 4 )

4 x 20 = 80

 

Total

100

 


Part – I (20 Marks)

 

1)                  Which of the following statements is correct about Variables?

 

A)    A variable is a name of the data to be used in your program.

B)     A variable is a name of the location in memory that can hold data.

C)    Variables are not case sensitive in Java, that is, variable N and n are the same.

D)    None of the above.

 

2)                  A variable which is accessible only within the function where it is declared is a:

 

A)    global variable    B)  local variable       C)  constant variable   D)  nonlocal variable

 

3)                  Programming languages such as FORTRAN, Visual Basic and Java are referred to as:

 

A)    High Level         B)  Low Level         C)  Both A and B       D)  Neither A nor B

 

4)                  Which of the following statements is TRUE about Interpreter?

 

A)    An interpreter is a general purpose language providing very efficient execution.

B)     An interpreter is a representation of the system being designed.

C)    An interpreter does the conversion line by line as the program is run.

D)    None of the above.

 

5)                  The starting point for the design of any computer program is

 

A)    defining the testing procedures for the program.

B)     a proper specification of its intended behavior.

C)    to design the flowchart for the program.

D)    to make documentation of the intended program.

 

6)                  Which of the following is NOT the primitive data types that built into Java?

 

A)    int                       B)  float                    C)  varchar                 D)  double

 

7)                  Which of the following is NOT a logical operator?

 

A)    &&                    B)  ||                         C)  !                           D)  >

 

8)                  A(n) _____________ is a piece of program code that represents or computes a value.

 

A)    macro                 B)  subroutine           C)  module                 D)   expression

 

9)                  Which of the following consists of a chain of objects of the same type, linked together by pointers from one object to the next?

 

A)    Array                 B)  Linked list           C)  Stack                  D)  Queue

 

10)              A programming technique in which a routine calls itself is refer to as:

 

A)    Inheritance          B)  Polymorphism     C)  Encapsulation       D)  Recursion


 

Part – II (80 Marks)

Question 1 (20 Marks)

a)                  What is a program?                                                                                                 [4 Marks]

Reference:

in order to execute a specific task , a computer can only work from a file which a set of instructions combined into , this file is called computer program.

b)                  What is the fundamental difference between machine code and the other programming languages? [4 Marks]

Reference:

machine code is consisted of ‘0’or ‘1’,it is very difficult to understand and write for programmer

 

c)                  Why Assembly Language is called a low level language? How do the higher level languages differ from Assembly Language?                                                                                                              [4 Marks]

Reference:

Assembly language is consisted of instructions and labels, there is a generally a one-to-one relationship between each assembly language instruction and a machine code instruction.

In high level language, the individual instructions are more powerful than the instructions in lower level language.much more complex formula may be expressed by using a single instruction in these high level language,

 

d)                  What is the difference between a compiler and an interpreter?                                  [4 Marks]

Reference:

Interpreter is a program that reads and executes instructions written in the form of a program,it is also the oldest type. Compiler is the program that translates source code programs into computer language

 

e)                  What information about a program is usually found in program specifications?           [4 Marks]

Reference:

1.    A description of the function of the program

2.    The input and output requirements

3.    Data storage methods

4.    File handling routines

5.    A description of any special processing or algorithms to be used in the program

6.    The output format.

Question 2 (20 Marks)

 

a)                  Describe how the decision table works and how it can be helpful to the programmer during the program development.                                                                                                          [6 Marks]

Reference:

A Decision Table is a table showing the various actions to be taken for different combinations of conditions.  The upper rows of the table specify the variables or conditions to be evaluated and the lower rows specify the corresponding action to be taken when a condition or a set of conditions is satisfied.

 

b)                  Explain why a program design is important to program development.                       [6 Marks]

Reference:

we have designed the logic of a program, it must be checked for correctness.  This must be done before coding begins - mistakes become harder and more costly to correct the longer they remain in existence.

c)                  Discuss any TWO of the distinct approaches to program design checking that exist. [8 Marks]

Reference:

1.      Dry RunThe purpose of a dry run is to see what the code actually does, not what it is supposed to do.  the checker (often the programmer) pretends to be the computer carrying out the program.  Take care to make a careful note (on paper) of the effect of each instruction in the program.

2.      Walk-throughThe author of the design describes what has been done and why.  Other participants raise queries or suggest possible pit-falls.  It is important that, at this stage, no attempt is made to solve the difficulties, only to recognise them.

                                                                                                                                                         

Question 3 (20 Marks)

 

Distinguish between the following

 

a)                  Subroutine and Macros

b)                  Function and Procedure

c)                  Syntactical errors and Logic errors

d)                  Bubble sort and Quick Sort

e)                  Stacks and Queues                                                                                            [5 x 4 Marks]

Reference:

 

a)

 Subroutine is a piece of program code which execute a specific function ,it is represented by fnction or method, used by other program by call its name.

Macros is a statement within a program which generates more than one machine instructions.  They are used where a common sequence of instructions are required, e.g. in reading or writing data from or to files.

 

b)

Function is consisted of function name ,a few statements which are grouped into a block, a function has parameters usually and has a return value.

Procedure is very similar to function, but it don’t return value ,it is called by its name, consisted of   a sequence of statements in its body.

 

c)

Syntactical errors  is the errors that can’t be satisfied with the syntax rules of java, for example ,the semicolon,’;’ is usually omitted by programmer when he write a statement.

Logic errors is the errors that can’t be executed properly by computer, for example in the loop statements , when a programmer omitted  the update statement, the condition will be satisfied for ever and the statements in loop body will be executed for ever.

 

d)

 Bubble sort is a sort method  and one of the simplest and the most popular algorithms. it is rather slow when applied to large arrays. The idea of bubble sort is to start at the top of the array. We compare each element to the next element. If its greater than that element then we swap the two. We pass through the array as many times as necessary to sort it. The smallest value bubbles up to the top of the array while the largest value sinks to the bottom.

Quick Sort is a recursive algorithm which turns out to be the fastest sorting algorithm in most situations.  Given a list of items, select any item from the list. This item is called the pivot. Move all the items that are smaller than the pivot to the beginning of the list, and move all the items that are larger than the pivot to the end of the list.

 

e)

A stack consists of a sequence of items, which should be thought of piled one on top of the other like a physical stack of boxes or cafeteria trays. Only the top item on the stack is accessible at any given time. LIFO(last in first out)先进后出

Queues are similar to stacks in that a queue consists of a sequence of items, and there are restrictions about how items can be added to and removed from the list. a queue has two ends, called the front and the back of the queue. FIFO(first in first out)先进先出

 

Question 4 (20 Marks)

 

a)                  By considering the following program, explain the behaviour of the for loop and the purpose of its components各个组成部分,指的是for循环中的三条语句.                                                                              [4 Marks]

 

class A

{

  public static void main(String[] args)

  {

    for(int i=0;i<5;i=i+1) System.out.println(i);

  }

}

Reference:

The for loop can be used to repeat a sequence of statements over and over, in the above example, i  is the loop variable and the value is initialized, the loop condition is the statement i<5, the update statement is i=i+1; the output of the example is to print the value 01234.

 

b)                  Rewrite the program A above using “do…while” loop.                                            [4 Marks]

Reference:

class A

{

  public static void main(String[] args)

  {

     int i=0;

     do{

           System.out.println(i);

           i=i+1;

                                                                                                                                             

     }while(i<5);

  }

}

 

c)                  Explain the difference between “while” loop and “do…while” loops by using proper examples. [6 Marks]

Reference:

1)“while” loop is to say that will repeat a statement over and over, but only so long as a specified condition remains true. it evaluates the boolean-expression firstly, if condition is satisified ,it is repeat the statements over and over,otherwise it skip the statements. only the condition is ture, the  body of the loop is executed.只有条件成立,循环语句才会被执行。

 

2) “do…while” loops is to say that it is very similar to the while statement, except that the word "while,"but it  first executes the body of the loop, then it evaluates the boolean expression,while the condition is true, it will repeat to executes the statements in the body of the loop. Whether or not the condition is true, the statements in the body of the loop will be executed  for one time.

不管条件是否成立,循环语句至少被执行一次。

d)                  Assume that we have Vehicle class declared as follows:

 

class Vehicle

{

      int registrationNumber;

      Person owner; //(assuming that person class has been defined)

      Void transferOwnerShip(Person newOwner)

      {

         ... ... ...

         ... ... ...

      }

      ... ...

}  

 

i.)      Write a Java code segment代码段 to declare Car class assuming that a car is a vehicle but it has distinct property “numberOfDoors”, which is of integer value.                                       [3 Marks]

Reference:

class car extends Vehicle

{

int registrationNumber;

int numberOfDoorsl;

              Person owner; //(assuming that person class has been defined)

Void transferOwnerShip(Person newOwner)

{

                                                       ... ... ...

                                                       ... ... ...

}

                                                         ... ...                                                                    

                                                           

}

ii.)     Write a Java code segment to declare Truck class assuming that a truck is a vehicle but it has distinct property “numberOfAxels”, which is of integer value.                                                       [3 Marks]

Reference:

class Truck  extends Vehicle

{

int registrationNumber;

int numberOfAxels;

              Person owner; //(assuming that person class has been defined)

Void transferOwnerShip(Person newOwner)

{

                                                       ... ... ...

                                                       ... ... ...

}

                                                         ... ...                                                                    

                                                           

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值