school of computing, engineering and ... exam...question 7 in java the variables of a class type are...

21
s SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177 Programming Time allowed: THREE hours: Answer: ALL questions Items permitted: There is no restriction on the paper-based items or items on a USB flash drive that the student may take into the examination. Items supplied: None CI101/2015/2016 Semester 1 Page 1 of 21 Printing date:05/12/2016

Upload: vantram

Post on 17-Mar-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

s

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016

CI101 / CI177

Programming

Time allowed: THREE hours: Answer: ALL questions Items permitted: There is no restriction on the paper-based

items or items on a USB flash drive that the student may take into the examination.

Items supplied: None

CI101/2015/2016 Semester 1 Page 1 of 21 Printing date:05/12/2016

Section A

Section A carries 50 marks in total.

Use the application provided to record the answer for each multiple choice question (Question 1 – Question 20) in section A of this exam paper. Remember to fill in your student number in the box provided for this (Highlighted initially with a reddish background) before existing from the application. Each question in Section A is worth 2.5 marks. Section A carries 50 marks in total. There is no penalty for answering a question wrongly.

Question 1 class Main { public static void main( String args[] ) { int page = 7; System.out.printf("Next page number = %2d\n", page + 1 ); } }

When executed the above Java application will print: (a) Next page number = 7

(b) Next page number = 8

(c) Next page number = Page + 1

(d) A run time error message

CI101/2015/2016 Semester 1 Page 2 of 21 Printing date:05/12/2016

Question 2 class Main { public static void main( String args[] ) { int free_gb = 4; if ( free_gb < 4 ) { System.out.println("Free memory low"); } if ( free_gb > 8 ) { System.out.println("Free memory high"); } } }

When executed the above Java application will print:

(a) No output will be produced (b) Free memory low (c) Free memory high (d) Free memory low Free memory high

CI101/2015/2016 Semester 1 Page 3 of 21 Printing date:05/12/2016

Question 3 class Main { public static void main( String args[] ) { int value = 0; value = 7 / 2; System.out.println( value ); value = 3 + 1/2; System.out.println( value ); value = 3; { int x = value; value = x++; } System.out.println( value ); value = 10 % 7; System.out.println( value ); } }

When executed the above Java application will:

(a) Print 3 once

(b) Print 3 two times

(c) Print 3 three times

(d) Print 3 four times.

CI101/2015/2016 Semester 1 Page 4 of 21 Printing date:05/12/2016

Question 4 class Main { public static void main( String args[] ) { int a = 1; int b = 2; int c = 3; if ( a != b ) System.out.println("OK"); if ( a <= b ) System.out.println("OK"); if ( b <= c && b >= a ) System.out.println("OK"); if ( ( b <= c || b >= a ) && true ) System.out.println("OK"); } }

When executed the above Java application will:

(a) print OK once.

(b) print OK two times

(c) print OK three times

(d) print OK four times.

CI101/2015/2016 Semester 1 Page 5 of 21 Printing date:05/12/2016

Question 5 class Main { public static void main( String args[] ) { int a = 1; int b = 2; int c = 3; for ( int i = c; i < b; i++ ) System.out.println("OK"); for ( int i = a; i <= c; i++ ) System.out.println("OK"); } }

When executed the above Java application will:

(a) print OK 2 times

. (b) print OK 3 times

(c) print OK 4 times

(d) print OK 5 times.

CI101/2015/2016 Semester 1 Page 6 of 21 Printing date:05/12/2016

Question 6 class Main { private static int year = 2016; public static void main( String args[] ) { if ( year > 2016 ) { int year = 2015; int i=1; while ( i < 2 ) { year += i; ++i; } System.out.println( year ); } } }

After executing the above code the result printed will be: (a) 2016

(b) 2017

(c) The program will not compile as the variable year is declared twice.

(d) When run, will produce no output

CI101/2015/2016 Semester 1 Page 7 of 21 Printing date:05/12/2016

Question 7

In Java the variables of a class type are introduced by field declarations. Thus an instance of a class (an object) will hold storage for each non static field that is declared within the class. If the field declaration is static it will be class wide and only one instance of the field will be held. Which of the following are valid field declarations? Assume that any necessary imports of classes have been made. (a) private final int LUCKY = 7 + 0;

(b) private static int state = 42;

(c) private ArrayList<String> names = new ArrayList<>();.

(d) All the above are valid field declarations

Question 8 In Java what is the largest whole number that can be held in an instance of a short:

(a) 127 (b) 32767 (c) 2147483647 (d) None of the above.

CI101/2015/2016 Semester 1 Page 8 of 21 Printing date:05/12/2016

Question 9 In the following Java program class Main { public static void main( String args[] ) { int count = 0; // Java statement System.out.println( value ); } } Which of the following replacements for the line “// Java statement”

Replacement Statement Java code S1 count++; S2 count = count + 1; S3 count += 1; S4 count =+ 1;

will cause the value 1 to be printed when the modified program is compiled and run.

(a) Only replacement statements S1, S2 or S3 will cause 1 to be printed. (b) Only replacement statements S2 or S4 will cause 1 to be printed. (d) Only replacement statements S1, S3 or S4 will cause 1 to be printed. (d) All statements (S1, S2, S3 or S4) will cause 1 to be printed.

CI101/2015/2016 Semester 1 Page 9 of 21 Printing date:05/12/2016

Question 10 If the class Box and Main are declared as follows:

class Box { private int contents = 0; public int contents() { return contents; } public Box inc() { contents++; return this; } public Box dec() { contents--; return this; } } class Main { public static void main( String args[] ) { String s = new String(); Box mysteryBox = new Box(); //new Box(); mysteryBox.inc().inc().dec().inc(); System.out.println( mysteryBox.contents() ); } }

What would be printed if the above class Main was run as a program?

(a) 0

(b) 1 (c) 2 (d) 4

CI101/2015/2016 Semester 1 Page 10 of 21 Printing date:05/12/2016

Question 11

Which of the following statements about the Java language is true?

(a) Using Java it is impossible to write an incorrect program. (b) You can always rewrite a for statement as a while statement. (c) An if statement must always have an else part. (d) A switch statement must always have a default label.

Question 12 Which of the following statements about the Java language is false?

(a) A method is invoked when you send a message to an object. (b) An object is an instance of a class. (c) Several objects may be instances of the same class. (d) A class may not contain two or more methods with the same name.

CI101/2015/2016 Semester 1 Page 11 of 21 Printing date:05/12/2016

Question 13 Which of the following statements about the Java language is true?

(a) An array may be extended by inserting a new element at the end. (b) An array index goes from 1 to the number of items in the array. (c) There is no way at run time of finding the number of elements in an array. (d) You can not use as an index to an array that is an instance of the class

String. Question 14 Which of the following statements about the Java language is true?

(a) An instance of the class String is immutable (can not be changed). (b) System.out.println( "hello".toUpperCase() );

Will print Hello (c) static int a = 2;

In subsequent code the value of a may not be changed. (d) char c = "Brighton".charAt("Brighton".length() );

Will put 'n' into the variable c. Question 15 Which of the following statements about the Java language is true?

(a) A constructor with no parameters must be explicitly defined in a class. (b) A constructor must have a void return type. (c) A class may have at most 1 (one) constructor. (d) A constructor in a class must have the same name as the class in which it is

part of.

CI101/2015/2016 Semester 1 Page 12 of 21 Printing date:05/12/2016

Question 16 Which of the following statements about the Java language is true?

(a) You do not need to use the class construct in a Java program that only

consists of a single line of Java code. (b) The class construct in Java helps simplify the process of reusing code

contained in an existing program by another program. (c) In a Java program a primitive type may be used anywhere a reference type

may be used. (d) The == operator should be used when comparing instances of the class

String. Question 17 Which of the following statements about the Java language is true?

(a) A private method is visible to a programmer, who wishes to call the method

from code that resides outside of the class. (b) All classes in Java must have a method with the signature:

public static void main( String args[] ) (c) An import statement can occur anywhere within a Java program. (d) A class may have an inner class.

Question 18 The debugger provided with the BlueJ IDE (Integrated Development Environment) will:

(a) Helps find syntax errors in your program. (b) Helps find compile time semantic errors in your program. (c) Helps find run-time logical errors in your program. (d) All of the above.

CI101/2015/2016 Semester 1 Page 13 of 21 Printing date:05/12/2016

Question 19 Which of the following statements about the Java language is true?

(a) A shallow copy is performed when you assign an instance of a reference type. (b) A byte is an example of a primitive type. (c) An array is an example of a reference type. (d) All of the above.

Question 20 In relationship to the standard conventions used for writing a Java program which of the following is true?

(a) The convention is that a mutable (can be changed at run-time) variable name

starts with a lower case letter. (b) The convention is that a method name starts with a lower case letter. (c) The convention is that a class starts with an upper case letter. (d) All of the above.

CI101/2015/2016 Semester 1 Page 14 of 21 Printing date:05/12/2016

Section B

Section B carries 50 marks

2.1 15 Marks

Brief A Java application has been written to input an integer number which represents the width of an inverted pyramid. This pyramid made of stars is then printed against a background of dots. The width of the pyramid must be odd and greater than or equal to 7. If this is not true, then the error message Size must be odd or >= 7 is to be printed.

Unfortunately the application contains in total 5 (Five) syntax, semantic or logical errors. Correct these errors in the application.

Remember to only correct the errors and do not rewrite the program.

A BlueJ project for this application named 2.1 is in the folder starting with ci101.exam at the top level on the h: drive. Using BlueJ correct these errors, so that the application produces the expected results.

Example input data

Commentary

Example Input

7 Width of pyramid picture.

Expected output ******* .*****. ..***.. ...*...

Special instructions

Remember any line printed that contains a # will not be considered as part of your answer. This is so that you can write out a meaningful prompt for any data (containing a # character) and this line of text will be ignored in the comparison of your answer.

The program

A printed copy of the Java application for question 2.1 (that contains the errors mentioned above) is on the following page.

P.T.O.

CI101/2015/2016 Semester 1 Page 15 of 21 Printing date:05/12/2016

The program for Question 2.1

//Please enter your student number // in this comment here: class Main { public static void main( String args[] ) { System.out.print("#Enter width of shape: "); int size = BIO.getInt(); if ( size%2 != 1 && size < 7 ) { System.out.println("Size must be odd or >= 7" ); } else { for ( int i=1; i<= size/2; i++ ) { print( '.', i ); print( '*', size-i*2 ); printnl( '.', i ); } } } /** * Print the character c howManyTimes * @param c The character to be printed * @param howManyTimes How many times to print c */ public static void print( char c, int howManyTimes ) { for ( int i=1; i<howManyTimes; i++ ) System.out.print( c ); } /** * Print the character c howManyTimes followed by a newline * @param c The character to be printed * @param howManyTimes How many times to print c */ public static void printnl( char c, int howManyTimes ) { print( c, howMany ); System.out.println(); }

CI101/2015/2016 Semester 1 Page 16 of 21 Printing date:05/12/2016

2.2 15 Marks

Brief

A Java application has been written to process data for units used by mobile phone users. Customers are charged a flat rate of 4p for each unit used. The data consists of: The mobile phone number The value of the last months units used The value of the current months units used

The data is terminated with a mobile phone number of 0. The data is to be checked for potential ‘problems’ and to highlight and report the number of ‘problems’ found in the data. HIGH

The data indicates that a high number of units have been used. High is defined as more than 1000 units used (£40)

OK The data looks normal. CHECK The data indicates that a negative

amount of units have been used. INACTIVE The data indicates that the mobile phone

has not been used. The results for each phone are reported on a single line, for example with the data: 0712345672 1000 999 0 Then the result printed would be: Number Previous Current Used Comment 0712345672 1000 999 CHECK Summary of phone data High = 0 OK = 0 Check = 1 Inactive = 0 Unfortunately the application contains (in total 5 errors) syntax, semantic and logic errors. A BlueJ project for this application named 2.2 is in the folder starting with ci101.exam at the top level on the h: drive. Using BlueJ correct these errors, so that the application produces the expected results.

P.T.O.

CI101/2015/2016 Semester 1 Page 17 of 21 Printing date:05/12/2016

Example input data

Commentary

Example Input

0712345678 0 10 0712345679 100 1000 0712345670 1000 3000 0712345671 1000 1000 0712345672 1000 999 0

Telephone number Previous reading Current reading Telephone number Previous reading Current reading Telephone number Previous reading Current reading Telephone number Previous reading Current reading Telephone number Previous reading Current reading Terminator

Expected output Number Previous Current Used Comment 0712345678 0 10 10 OK 0712345679 100 1000 900 OK 0712345670 1000 3000 2000 HIGH 0712345671 1000 1000 INACTIVE 0712345672 1000 999 CHECK Summary of phone data High = 1 OK = 2 Check = 1 Inactive = 1

Special instructions

Remember any line printed that contains a # will not be considered as part of your answer. This is so that you can write out a meaningful prompt for any data (containing a # character) and this line of text will be ignored in the comparison of your answer.

The program

A printed copy of the Java application for question 2.2 (that contains the errors mentioned above) is on the following page.

The format %010d in a printf statement means print an instance of a long (or value that can be converted to a long) in a field width of 10 with leading 0’s

P.T.O.

CI101/2015/2016 Semester 1 Page 18 of 21 Printing date:05/12/2016

The program for Question 2.2

class Main { public static void main( String args[] ) { System.out.printf("%10s %8s %8s %8s %8s\n", "Number", "Previous", "Current", "Used", "Comment" ); System.out.print("#Enter phone number : " ); long number = BIO.getInt(); int high = 0, ok = 0, inactive = 0, check = 0; while ( number >= 0 ) { System.out.print("#Enter previous reading: " ); int previous = BIO.getInt(); System.out.print("#Enter current reading : " ); int current = BIO.getInt(); int used = current - previous; System.out.printf("%010d %8d %8d ", number, previous, current ); if ( used > 0 ) { if ( used >= 1000 ) { System.out.printf( "%8d %8s", used, "HIGH" ); high++; } else { System.out.printf( "%8d %8s", used, "OK" ); ok--; } } else { if ( used <= 0 ) { System.out.printf("%8s %8s", "", "CHECK" ); check++; } if ( used == 0 ) { System.out.printf("%8s %8s", "", "INACTIVE" ); inactive++; } } System.out.println(); System.out.print("#Enter phone number : " ); number = BIO.getInt(); } System.out.printf("Summary of phone data\n" + "High = %6d\nOK = %6d\n" + "Check = %6d\nInactive = %6d\n", high, OK, check, inactive ); } }

CI101/2015/2016 Semester 1 Page 19 of 21 Printing date:05/12/2016

2.3 20 Marks

Brief

Write a Java application that reads in a value representing the width and height of a square picture that displays on a background of dots a cross made up of @ symbols. The picture includes a frame of stars. This style of picture is often referred to as ASCII art. For example if the number input was 5 then the following picture would be displayed: ASCII art picture size 5 ***** *@.@* *.@.* *@.@* ***** If the input value is not an odd number, then the following error message (if for example 8 were input) would be displayed: Error size (8) must be an odd number If the input value is less than 0 then the following error message (if for example -81 were input) would be displayed: Error size (-81) must be greater than 0 The data is terminated by the number 0 (Zero). A BlueJ project for this application named 2.3 is in the folder starting with ci101.exam at the top level on the h: drive. Using BlueJ construct the application to produce the ASCII art picture.

Example input data Commentary Example Input

1 9 0

Size of square Size of square Terminator

Expected output ASCII art picture size 1 * ASCII art picture size 9 ********* *@.....@* *.@...@.* *..@.@..* *...@...* *..@.@..* *.@...@.* *@.....@* *********

CI101/2015/2016 Semester 1 Page 20 of 21 Printing date:05/12/2016

Special instructions

Remember any line printed that contains a # will not be considered as part of your answer. This is so that you can write out a meaningful prompt for any data (containing a # character) and this line of text will be ignored in the comparison of your answer. Remember, you must be able to draw the picture for any odd number greater than 0.

CI101/2015/2016 Semester 1 Page 21 of 21 Printing date:05/12/2016