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

Similar documents
Birkbeck (University of London) Software and Programming 1 In-class Test Mar 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

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

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

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

Loops and Expression Types

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

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

*Java has included a feature that simplifies the creation of

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 #0 Fundamentals of Computer Science I Fall 2013

University of Palestine. Mid Exam Total Grade: 100

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

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

CS 101 Spring 2007 Midterm 2 Name: ID:

Arrays. Friday, November 9, Department of Computer Science Wellesley College

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

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

Oct Decision Structures cont d

Input-Output and Exception Handling

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

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

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

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

( &% class MyClass { }

Problem Grade Total

Programming - 2. Common Errors

Final Examination Semester 2 / Year 2011

COS 126 General Computer Science Spring Written Exam 1

Arrays. Eng. Mohammed Abdualal

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

Loops. CSE 114, Computer Science 1 Stony Brook University

d. If a is false and b is false then the output is "ELSE" Answer?

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

Practice Questions for Chapter 9

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

Chapter 1: Introduction to Computers, Programs, and Java

CS111: PROGRAMMING LANGUAGE II

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

1.Which four options describe the correct default values for array elements of the types indicated?

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

CS171:Introduction to Computer Science II

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CSE 143 SAMPLE MIDTERM

WES-CS GROUP MEETING #9

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

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

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

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)

CS171:Introduction to Computer Science II

Lara Technologies Special-Six Test

CS111: PROGRAMMING LANGUAGE II

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)

Instance Method Development Demo

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Class, Variable, Constructor, Object, Method Questions

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Programming Problems 22nd Annual Computer Science Programming Contest

Fundamentals of Programming Data Types & Methods

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

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

Computer Science 1 Ah

CS180. Exam 1 Review

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

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

CS 101 Fall 2006 Midterm 3 Name: ID:

ESC101 : Fundamental of Computing

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

Lecture Set 4: More About Methods and More About Operators

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

CS 101 Exam 2 Spring Id Name

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

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

public static boolean isoutside(int min, int max, int value)

Arrays and Basic Algorithms

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Selected Questions from by Nageshwara Rao

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

Introduction to Computer Science II (ITI 1121) Midterm Examination

AP CS Unit 8: Inheritance Exercises

CS 302 Week 9. Jim Williams

JAVA OPERATORS GENERAL

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

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

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

PASS4TEST IT 인증시험덤프전문사이트

Programming with Java

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Transcription:

Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 22 Mar 2018 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 y = 0; for (int x = 4; x <= 12; x += 2) { y += x; System.out.println(y); (5 marks) Workings: COIY018H5 Page 1 of 10 c Birkbeck College 2018

2. Let country be a variable of type String that possibly contains null. Write a Java expression (of type boolean) that is evaluated to true if country is a non-empty string (recall that the empty string is ""); and evaluated to false otherwise. The expression should never throw a NullPointerException. (5 marks) 3. What output is produced when the following Java program fragment is executed in each case? (a) a is 1. (b) a is 2. (c) a is 3. You should show your workings. if (a >= 2) if (a == 3) System.out.print("T"); else System.out.print("D"); else System.out.print("U"); (5 marks) COIY018H5 Page 2 of 10 c Birkbeck College 2018

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

5. A run is a sequence of adjacent repeated values. Implement a method public static int getlongestrunlength(int[] v) that computes and returns the length of the longest run in an array. For example, the longest run in the array with elements 1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 3, 1 has length 4 because the array contains 4 consecutive 2s. (20 marks) COIY018H5 Page 4 of 10 c Birkbeck College 2018

6. Implement a method public static double weightedsum(double[] values, double[] weights) that, given two arrays values and weights of floating-point numbers, returns the weighted sum i values i weights i : for example, if values is { 1, 2, 3 and weights is { 0.1, 0.2, 0.7, then the method returns 1 0.1 + 2 0.2 + 3 0.7 = 2.6. Assume that the two parameter arrays are of the same length. (10 marks) COIY018H5 Page 5 of 10 c Birkbeck College 2018

7. What output is produced when the following Java program is executed? public class E18 1 { public static void main(string args[]) { int[] a = { 1, 3, 2, 1 ; System.out.println(g(a, 1) == g(a, 2)? "yes" : "no"); public static int g(int a[], int i) { int idx = a[i]; idx = a[idx]; return a[idx]; You should show your workings. (10 marks) Workings: COIY018H5 Page 6 of 10 c Birkbeck College 2018

8. Suppose you have declared an interface IntegerExpression as follows: public interface IntegerExpression { int getvalue(); (a) Write a class IntegerConstant that implements interface IntegerExpression. The class should have a constructor and an instance variable, val, of type int. The constructor takes an int as a parameter and stores it in val. (7 marks) (b) In class IntegerConstant, implement method getvalue() that returns the value stored in val. (4 marks) (c) Write a class Addition that implements interface IntegerExpression. The constructor of Addition should take two instances of IntegerExpression, the left- and right-hand side arguments of the + operation, respectively. Use suitable instance variables to store references to the two subexpressions. Method getvalue() should return the result of adding the values provided by the two subexpressions. (6 marks) (d) Write a class Multiplication that implements interface IntegerExpression in such a way that its method getvalue() returns the result of multiplying the values provided by the two subexpressions; see (c). (6 marks) (e) Override method tostring() in the three classes, IntegerConstant, Addition and Multiplication, in such a way that they render the expression in reverse Polish notation, where the operators follow their operands, and, if there are multiple operations, operators are given immediately after their second operands. For example, (1 + 2) 3 is 1 2 + 3 in reverse Polish notation. In other words, the methods tostring() should be implemented so that after executing the following Java fragment: IntegerExpression p1 = new Addition(new IntegerConstant(1), new IntegerConstant(2)); IntegerExpression p2 = new Multiplication(p1, new IntegerConstant(3)); the call p2.tostring() would return "1 2 + 3 *". Note that "1 2 +" would be produced by the call to p1.tostring(). (10 marks) COIY018H5 Page 7 of 10 c Birkbeck College 2018

COIY018H5 Page 8 of 10 c Birkbeck College 2018

COIY018H5 Page 9 of 10 c Birkbeck College 2018

9. Find five compile-time errors in the following class: public class Q18 1 { public void main(string[] args) { final boolean help = false; int i; while (i < args.length() &&!help) { if (args[i] == "--help") help = "true"; else i++; return i; How would you correct the errors you have found (with as few changes as possible)? (7 marks) COIY018H5 Page 10 of 10 c Birkbeck College 2018