SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Similar documents
SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 2 EXAMINATIONS 2014/2015 CI101/CI101H. Programming

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 2 EXAMINATIONS 2013/2014 CI101/CI101H. Programming

In the following description the h: drive is used to indicate the directory in which during the actual ci101 exam the files used will reside.

Section 2.2 Your First Program in Java: Printing a Line of Text

APCS Semester #1 Final Exam Practice Problems

Section 2.2 Your First Program in Java: Printing a Line of Text

Full file at

Programming with Java

CS 139 Practice Midterm Questions #2

St. Edmund Preparatory High School Brooklyn, NY

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Simple Java Reference

Midterm Examination (MTA)

Introduction to Java Applications

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

PROGRAMMING FUNDAMENTALS

Do not start the test until instructed to do so!

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

Review for Test 1 (Chapter 1-5)

Course Outline. Introduction to java

CS 231 Data Structures and Algorithms, Fall 2016

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Full file at

CMSC 132: Object-Oriented Programming II

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CS141 Programming Assignment #6

2.8. Decision Making: Equality and Relational Operators

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

C0MP1911 Final Exam 1337 Computing 1

In Java there are three types of data values:

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Building Java Programs Chapter 2

This is a copy of the notes used in the module CI01.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

Introduction to the coursework for CI228

COMP102: Test 1 Model Solutions

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Curriculum Map Grade(s): Subject: AP Computer Science

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Objects and Classes -- Introduction

Reminder the scope of a variable is the part of the program where that variable is visible Will this compile?

Selected Questions from by Nageshwara Rao

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

1.00 Lecture 8. Using An Existing Class, cont.

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Name CIS 201 Midterm II: Chapters 1-8

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Ticket Machine Project(s)

CSE 11 Midterm Fall 2008

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Key Differences Between Python and Java

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

You must bring your ID to the exam.

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Getting started with Java

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

CSE 1223: Exam II Autumn 2016

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Use the scantron sheet to enter the answer to questions (pages 1-6)

Introduction to Programming Using Java (98-388)

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Logic & program control part 2: Simple selection structures

Software and Programming 1

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

COMP102: Test July, 2006

Inf1-OOP. Encapsulation and Collections. Perdita Stevens, adapting earlier version by Ewan Klein. March 2, School of Informatics

Java Bytecode (binary file)

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

SSOL Language Reference Manual

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

M257 Putting Java to work

COMP 401 Spring 2013 Midterm 1

AP Computer Science Summer Work Mrs. Kaelin

Semester 1 CI101/CI177. Java

Solutions to Quiz 1 (March 14, 2016)

CS111: PROGRAMMING LANGUAGE II

Full file at

Objects and Classes Lecture 2

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Datatypes, Variables, and Operations

Basics of programming

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CS 177 Week 15 Recitation Slides. Review

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

EC312 Chapter 4: Arrays and Strings

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Java & Fundamental Data Types

CSCE 145 Exam 1 Review. This exam totals to 100 points. Follow the instructions. Good luck!

CS141 Programming Assignment #8

Lecture Notes for CS 150 Fall 2009; Version 0.5

ECE264 Fall 2013 Exam 3, November 20, 2013

CpSc 1111 Lab 4 Formatting and Flow Control

Transcription:

s SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177 Programming Time allowed: THREE hours: Answer: ALL questions Items permitted: Items supplied: There is no restriction on the paper-based items or items on a USB flash drive that the student may take into the examination. 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 int page = 7; System.out.printf("Next page number = %2d\n", page + 1 ); When executed the above Java application will print: Next page number = 7 Next page number = 8 Next page number = Page + 1 A run time error message CI101/2015/2016 Semester 1 Page 2 of 21 Printing date:05/12/2016

Question 2 class Main 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: No output will be produced Free memory low Free memory high Free memory low Free memory high CI101/2015/2016 Semester 1 Page 3 of 21 Printing date:05/12/2016

Question 3 class Main 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: Print 3 once Print 3 two times Print 3 three times Print 3 four times. CI101/2015/2016 Semester 1 Page 4 of 21 Printing date:05/12/2016

Question 4 class Main 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: print OK once. print OK two times print OK three times print OK four times. CI101/2015/2016 Semester 1 Page 5 of 21 Printing date:05/12/2016

Question 5 class Main 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: print OK 2 times. print OK 3 times print OK 4 times 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; 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: 2016 2017 The program will not compile as the variable year is declared twice. 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. private final int LUCKY = 7 + 0; private static int state = 42; private ArrayList<String> names = new ArrayList<>();. 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: 127 32767 2147483647 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 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. Only replacement statements S1, S2 or S3 will cause 1 to be printed. Only replacement statements S2 or S4 will cause 1 to be printed. Only replacement statements S1, S3 or S4 will cause 1 to be printed. 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 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? 0 1 2 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? Using Java it is impossible to write an incorrect program. You can always rewrite a for statement as a while statement. An if statement must always have an else part. A switch statement must always have a default label. Question 12 Which of the following statements about the Java language is false? A method is invoked when you send a message to an object. An object is an instance of a class. Several objects may be instances of the same class. 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? An array may be extended by inserting a new element at the end. An array index goes from 1 to the number of items in the array. There is no way at run time of finding the number of elements in an array. 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? An instance of the class String is immutable (can not be changed). System.out.println( "hello".touppercase() ); Will print Hello static int a = 2; In subsequent code the value of a may not be changed. 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 constructor with no parameters must be explicitly defined in a class. A constructor must have a void return type. A class may have at most 1 (one) constructor. 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? You do not need to use the class construct in a Java program that only consists of a single line of Java code. The class construct in Java helps simplify the process of reusing code contained in an existing program by another program. In a Java program a primitive type may be used anywhere a reference type may be used. 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 private method is visible to a programmer, who wishes to call the method from code that resides outside of the class. All classes in Java must have a method with the signature: An import statement can occur anywhere within a Java program. A class may have an inner class. Question 18 The debugger provided with the BlueJ IDE (Integrated Development Environment) will: Helps find syntax errors in your program. Helps find compile time semantic errors in your program. Helps find run-time logical errors in your program. 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 shallow copy is performed when you assign an instance of a reference type. A byte is an example of a primitive type. An array is an example of a reference type. All of the above. Question 20 In relationship to the standard conventions used for writing a Java program which of the following is true? The convention is that a mutable (can be changed at run-time) variable name starts with a lower case letter. The convention is that a method name starts with a lower case letter. The convention is that a class starts with an upper case letter. All of the above. CI101/2015/2016 Semester 1 Page 14 of 21 Printing date:05/12/2016

2.1 15 Marks Section B Section B carries 50 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 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 OK CHECK INACTIVE The data indicates that a high number of units have been used. High is defined as more than 1000 units used ( 40) The data looks normal. The data indicates that a negative amount of units have been used. 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 Example input data 0712345678 0 10 0712345679 100 1000 0712345670 1000 3000 0712345671 1000 1000 0712345672 1000 999 0 Commentary 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 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 Example 1 Input 9 0 Expected output ASCII art picture size 1 * ASCII art picture size 9 ********* *@...@* *.@...@.* *..@.@..* *...@...* *..@.@..* *.@...@.* *@...@* ********* Commentary Size of square Size of square Terminator 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