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

Similar documents
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

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

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

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

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

LAMBDA EXPRESSIONS. Summer 2018

Key Differences Between Python and Java

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

Computational Expression

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Oct. 3 fixup - Here are notes and codes in the proper order that they should be.

Your first C++ program

Introduction Basic elements of Java

Branching and Boolean Expressions

1B1b Classes in Java Part I

VARIABLES AND TYPES CITS1001

Branching and Boolean Expressions

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Datatypes, Variables, and Operations

COS 126 General Computer Science Spring Written Exam 1

Introduction to Programming Using Java (98-388)

Java Bytecode (binary file)

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

CE221 Programming in C++ Part 1 Introduction

Introduction to Software Development (ISD) Week 3

BTE2313. Chapter 2: Introduction to C++ Programming

Java Identifiers, Data Types & Variables

AP CS Fall Semester Final

Unit 2: Java in the small

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

Object oriented programming C++

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

CPS122 Lecture: From Python to Java

An Introduction to Processing

CSI33 Data Structures

Getting started with Java

9 Working with the Java Class Library

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 13 th February

Software and Programming 1

Object-oriented programming in...

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Pace University. Fundamental Concepts of CS121 1

CSE 1223: Exam II Autumn 2016

Array. Prepared By - Rifat Shahriyar

( &% class MyClass { }

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Arrays and Basic Algorithms

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Chapter 2: Using Data

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

STUDENT LESSON A12 Iterations

Chapter 1: Introduction to Computers, Programs, and Java

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to C++ with content from

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

Date: Dr. Essam Halim

CSE 114 Computer Science I

Lecture Set 2: Starting Java

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

4. If the following Java statements are executed, what will be displayed?

Chapter 6 Single-dimensional Arrays

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lecture Set 2: Starting Java

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Exam 2, Version 2. For the following code, mark True or False for statements 1.8 to 1.10.

1.00 Lecture 6. Java Methods

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

COMPUTER APPLICATIONS

Visual C# Instructor s Manual Table of Contents

Full file at

CSc Introduction to Computing

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

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

SSOL Language Reference Manual

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

CS 305j Midterm 2 Fall 2006

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Typescript on LLVM Language Reference Manual

2. The object-oriented paradigm

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Java Coding 3. Over & over again!

Oct Decision Structures cont d

The for Loop. Lesson 11

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

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

AP CS Unit 3: Control Structures Notes

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Transcription:

Birkbeck (University of London) Software and Programming 1 In-class Test 1.1 9 Feb 2017 Student Name Student Number Answer all questions 1. Consider the following sequence of Java statements: int m = 3; int n = 9 - m; m = 4 + n / m * 2 + 3; n = 32 / m + m * 3; What is the value of n after these statements are executed? Show your workings. (7 marks) 35 Workings: (a) n = 6, (b) m = 4 + 2 * 2 + 3 = 11, (c) n = 2 + 33 = 35 COIY018H5 Page 1 of 7 c Birkbeck College 2017

2. Given a variable word of type String, write an expression of type boolean, which is evaluated to true if the word contains between 4 and 8 (inclusive) characters and comes lexicographically before "Birkbeck", and to false otherwise. (8 marks) word.length() >= 4 && word.length() <= 8 && word.compareto("birkbeck") < 0 sp1-02-17.pdf / p. 18 and p. 19 3. What is the scope of a variable? (a) the statement in which the variable is declared; (b) the statement in which the variable is initialised; (c) the part of the program where the variable is visible; (d) another name for the type of the variable. (3 marks) (c) sp1-03-17.pdf, p. 15 4. Which of the following are valid Java identifiers (i.e., possible names of variables/methods)? (a) Int (b) def (c) R2D2 (d) 3PO (e) double float (f) char (g) function (a), (b), (c), (e), (g) sp1-01-17.pdf / p. 20 (7 marks) COIY018H5 Page 2 of 7 c Birkbeck College 2017

5. Identify and explain five compile-time errors in the following Java code: public class f00 bar { public void hi 2(int q) double d = 170; String s = 42; while (d < 200) { String b = "d + d = s"; //// now check parameter if (3 == q+1) d = d + 1 b =+ "aa"; print("d = s"); How would you correct the errors you have found? (10 marks) public class f00 bar { public void hi 2(int q) { // 1: curly bracket missing double d = 170; String s = "42"; // 2: wrong literal type while (d < 200) { String b = "d + d = s"; //// now check parameter if (3 == q+1) d = d + 1; // 3: missing semicolon b += "aa"; // 4: =+ is not applicable to String System.out.println("d = s"); // 5: unknown method print COIY018H5 Page 3 of 7 c Birkbeck College 2017

6. Implement a method to determine the type of electromagnetic radiation of a given wavelength: radiation with wavelengths up to 10 nm is called X-rays; radiation with wavelengths between 10 nm and 400 nm is called ultraviolet light; radiation with wavelengths between 400 nm and 800 nm is called visible light; radiation with wavelengths of at least 800 nm is called infrared light. The method should take one argument of type double, the wavelength in nm, and return the kind of radiation, of type String. (10 marks) public static String radiationtype(double wavelength) { if (wavelength <= 10) return "X-rays"; if (wavelength <= 400) return "ultraviolet light"; if (wavelength <= 800) return "visible light"; return "infrared light"; COIY018H5 Page 4 of 7 c Birkbeck College 2017

7. What is printed as a result of executing the following fragment of code? int x = 1; int y = x + 2; while (x < 50) { x = x * y; System.out.println(x + 1); y = 2 * y - 3; Show your workings. 4 10 28 82 Workings: x y x < 50 new x printout x + 1 new y 1 3 true 3 4 3 3 3 true 9 10 3 9 3 true 27 28 3 27 3 true 81 82 3 81 3 false (10 marks) 8. What are the type and the value of the following expression s.length() + 3 > b && b % 2 > 0? s.length() + 1.5 : b / 2.0 if s is "123" and b is 5? double and 4.5 (because both arguments of the logical AND are true) (5 marks) COIY018H5 Page 5 of 7 c Birkbeck College 2017

9. Implement a method that returns true if its argument of type String is a word that (a) consists of upper-case letters only and (b) contains at least one occurrence of letter Q. (20 marks) public static boolean isqword(string s) { boolean foundq = false; for (int i = 0; i < s.length(); i++) { char c = s.charat(i); if (! Character.isUpperCase(c)) return false; if (c == Q ) foundq = true; return foundq; COIY018H5 Page 6 of 7 c Birkbeck College 2017

10. (a) Transform the for loop in the following fragment of code into a while loop. String s = "1"; for (int i = 2; i <= n; i++) if (n % i == 0) s = s + " " + i; System.out.println("result: " + s); (b) Suppose the type of n is int and its value is 18. What is printed out as a result of executing this fragment of code? (c) Explain the action of this fragment of code (for a given positive integer n). (20 marks) (a) String s = "1"; int i = 2; // first argument of the for loop while (i <= n) { // second argument of the for loop turns into // the while condition if (n % i == 0) s = s + " " + i; i++; // do not forget the third argument of the for loop // and the curly brackets System.out.println("result: " + s); (b) The output is: result: 1 2 3 6 9 18 (c) The program prints the divisors of n (=18; in general, any positive integer). COIY018H5 Page 7 of 7 c Birkbeck College 2017