Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Similar documents
Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Loops and Expression Types

*Java has included a feature that simplifies the creation of

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

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

CS170 Introduction to Computer Science Midterm 2

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Arrays. Eng. Mohammed Abdualal

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Introduction to Programming Using Java (98-388)

CS180. Exam 1 Review

University of Palestine. Mid Exam Total Grade: 100

The Java language has a wide variety of modifiers, including the following:

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Tutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I

Loops. CSE 114, Computer Science 1 Stony Brook University

Problem Grade Total

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

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

Java Fundamentals (II)

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Java Bytecode (binary file)

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

COS 126 Midterm 1 Written Exam Fall 2011

Fundamentals of Programming Data Types & Methods

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CS 231 Data Structures and Algorithms, Fall 2016

CS 101 Fall 2006 Midterm 3 Name: ID:

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

THIS EXAMINATION PAPER MUST NOT BE REMOVED FROM THE EXAMINATION ROOM

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

Following is the general form of a typical decision making structure found in most of the programming languages:

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

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

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Computer Science is...

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Programming with Java

CSE 143 SAMPLE MIDTERM

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

Object Oriented Programming. Java-Lecture 6 - Arrays

COE318 Lecture Notes Week 6 (Oct 10, 2011)

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

I. True/False: (2 points each)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

AP Computer Science Midterm Review Part 1

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Chapter 1: Introduction to Computers, Programs, and Java

Final Exam. COMP Summer I June 26, points

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

H212 Introduction to Software Systems Honors

Class, Variable, Constructor, Object, Method Questions

PROGRAMMING FUNDAMENTALS

AP CS Unit 7: Arrays Exercises

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Spring University of New Mexico CS152 Midterm Exam. Print Your Name

CSE 1223: Exam II Autumn 2016

Introduction to Java

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

CS 201, Fall 2016 Oct 28th Exam 2. Question 1. [5 points] Consider the following static method:

COS 126 General Computer Science Spring Written Exam 1

CS 101 Exam 2 Spring Id Name

Final Examination Semester 2 / Year 2011

Solutions to Sample JAC444 Midterm Test

CS111: PROGRAMMING LANGUAGE II

Full file at

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CLASS DESIGN. Objectives MODULE 4

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CS 101 Spring 2007 Midterm 2 Name: ID:

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CS18000: Problem Solving And Object-Oriented Programming

Transcription:

Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 16 Mar 2017 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment is executed? You should show your workings. int x = 2; int y = 2; while (x < 10) { y += x; x += 2; System.out.println(y); (5 marks) 22 Workings: before after x y condition y x 2 2 2 < 10 is true 4 4 4 4 4 < 10 is true 8 6 6 8 6 < 10 is true 14 8 8 14 8 < 10 is true 22 10 10 22 10 < 10 is false COIY018H5 Page 1 of 10 c Birkbeck College 2017

2. Let country be a variable of type String. Write a Java expression (of type boolean) that is evaluated to true if country is either an empty string ("") or null; and evaluated to false otherwise. (5 marks) country == null country.equals("") 3. What output is produced when the following Java program fragment is executed in each case? (a) a is -1. (b) a is 0. (c) a is 1. You should show your workings. if (a >= 0) if (a == 0) System.out.print("Z"); else System.out.print("P"); System.out.print("N"); (a) N (b) ZN (c) PN the else belongs to the second if (5 marks) COIY018H5 Page 2 of 10 c Birkbeck College 2017

4. What output is produced when the following Java program is executed? public class D17 1 { public static void main(string[] args) { for(int i = 1; i <= 5; i++) { for(int j = 0; j <= 5; j++) if (i <= 3 && j <= 6 - i && j >= i - 1) System.out.print("x"); else System.out.print("."); System.out.println(); You should show your workings. xxxxxx.xxxx...xx........ (5 marks) COIY018H5 Page 3 of 10 c Birkbeck College 2017

5. Implement a method public static int largestdiff(int[] arr) that takes an array arr of integer numbers and returns the largest difference between the consecutive elements of arr. Assume that arr contains at least two elements. For instance, if the input array is { 0, 1, -2, 4, 7, the differences between the consecutive elements are: 1 0, {{ 2 1, {{ 4 ( 2), {{ 7 4, {{ and so the output is 6. = 1 = 3 = 6 = 3 (20 marks) public static int largestdiff(int[] arr) { int result = arr[1] - arr[0]; for (int i = 2; i < arr.length; i++) { int diff = arr[i] - arr[i-1]; if (diff > result) result = diff; return result; alternatively: public static int largestdiff(int[] arr) { int result = arr[1] - arr[0]; for (int i = 1; i < arr.length - 1; i++) { int diff = arr[i+1] - arr[i]; if (diff > result) result = diff; return result; COIY018H5 Page 4 of 10 c Birkbeck College 2017

6. Implement a method public static void normalise(double[] v) that, given an array v of floating-point numbers, divides each of its elements by the sum of elements of v: for example, if the parameter array is { 0, 1, 2, 3, then the method modifies the array so that it contains because the sum of elements is 6. { 0, 0.166..., 0.333..., 0.5 (10 marks) public static void normalise(double[] v) { double sum = 0; for (double d : v) // alternatively: for (int i = 0; i < v.length; i++) sum += d; // sum += v[i]; for (int i = 0; i < v.length; i++) v[i] /= sum; COIY018H5 Page 5 of 10 c Birkbeck College 2017

7. What output is produced when the following Java program is executed? public class E17 1 { public static void main(string args[]) { int[] a = { 1, 1, 2 ; int[] b = { 2, 0, 0 ; System.out.println(g(a) == g(b)? "yes" : "no"); public static int g(int a[]) { int idx = a[0]; idx = a[idx]; return a[idx]; You should show your workings. no Workings: get: (10 marks) When the method g is called in g(a), a in g refers to the array { 1, 1, 2 and so, we int idx = a[0]; becomes 1 idx = a[1]; becomes 1 return a[1]; returns 1 When the method g is called in g(b), a in g refers to the array { 2, 0, 0 and so, we get: int idx = a[0]; becomes 2 idx = a[2]; becomes 0 return a[0]; returns 2 As 1 2, the method main prints "no". COIY018H5 Page 6 of 10 c Birkbeck College 2017

8. Suppose you have declared an interface Expression as follows: (a) public interface Expression { String gettype(); Write a class Literal that implements interface Expression. The class should have three constructors and two instance variables, val and tp, of type String. The first constructor takes an int as a parameter and sets tp to "int". The second constructor takes a double as a parameter and sets tp to "double". The third constructor takes a String as a parameter and sets tp to "string". Each of the constructors stores the parameter in val. (10 marks) (b) In class Literal, implement methods getvalue and gettype that return the values stored in val and tp, respectively. (4 marks) (c) Write a class OperationPlus that implements interface Expression. The constructor of OperationPlus should take two instances of Expression, the left- and righthand side arguments of the + operation, respectively. Use suitable instance variables. Method gettype should first get the types of the left- and right-hand side arguments of the + operation and then return "int" if both types are "int"; "double" if one type is "double" and the other is either "double" or "int"; "string" if one of the types is "String"; null otherwise. (11 marks) (d) Override method tostring in classes Literal and OperationPlus in such a way that after executing the following Java fragment: Expression p1 = new OperationPlus(new Literal(1), new Literal(2.0)); Expression p2 = new OperationPlus(p1, new Literal("s")); the call p2.tostring() would return "((1@int + 2.0@double) + s@string)". In other words, tostring of Literal should return the value and its type separated by @; method tostring of OperationPlus should return the result of applying tostring to both arguments of the + operations, which are separated by " + " and enclosed in brackets. (8 marks) COIY018H5 Page 7 of 10 c Birkbeck College 2017

(a) public class Literal implements Expression { private String val; private String tp; public Literal(int x) { val = "" + x; tp = "int"; public Literal(double x) { val = "" + x; tp = "double"; public Literal(String x) { val = x; tp = "string"; (b) public String getvalue() { return val; public String gettype() { return tp; COIY018H5 Page 8 of 10 c Birkbeck College 2017

(c) public class OperationPlus implements Expression { private Expression left, right; public OperationPlus(Expression left, Expression right) { this.left = left; this.right = right; public String gettype() { String t1 = left.gettype(); String t2 = right.gettype(); if (t1.equals("int") && t2.equals("int")) return "int"; if (t1.equals("double") && t2.equals("double") t1.equals("double") && t2.equals("int") t1.equals("int") && t2.equals("double")) return "double"; if (t1.equals("string") t2.equals("string")) return "string"; return null; (d) In class Literal: public String tostring() { return val + "@" + tp; In class OperationPlus: public String tostring() { return "(" + left.tostring() + " + " + right.tostring() + ")"; or public String tostring() { return "(" + left + " + " + right + ")"; COIY018H5 Page 9 of 10 c Birkbeck College 2017

9. Find five compile-time errors in the following Java class declaration: public class Y17 1 { private static final int x = null; public boolean f(string args) { Y17 1 y = new Y17 1(args); y.x = 21; String x = args; int[] array = { 22, 23, 24 ; for (int d : array) x = x + d; System.out.print("current d is " + d); return array[array[25]]; How would you correct the errors you have found (with as few changes as possible)? (7 marks) public class Y17 1 { private static int x = 0; // 0, not null public int f(string args) { // return type Y17 1 y = new Y17 1(args); // default constructor has no arguments y.x = 21; // drop final from declaration of x to write there String x = args; int[] array = { 22, 23, 24 ; for (int d : array) { // need {... ; otherwise, the scope of d x = x + d; // is limited to this statement System.out.print("current d is " + d); return array[array[25]]; COIY018H5 Page 10 of 10 c Birkbeck College 2017