CSC 1351 The Twelve Hour Exam From Hell

Similar documents
CSC Java Programming, Fall Java Data Types and Control Constructs

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

CSC 1351: Exam 2: Midterm

CSC 1351: Exam On Everything

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

CLASS DESIGN. Objectives MODULE 4

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Write the letter of the correct answer for each problem in the blanks on the answer form.

CSC 1214: Object-Oriented Programming

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Final Exam CS 251, Intermediate Programming December 13, 2017

Midterm Exam CS 251, Intermediate Programming March 12, 2014

APCS Semester #1 Final Exam Practice Problems

Polymorphism. return a.doublevalue() + b.doublevalue();

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

Introduction to Programming Using Java (98-388)

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

UMBC CMSC 331 Final Exam

Chapter 4: Writing Classes

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

1B1b Classes in Java Part I

Practice Questions for Chapter 9

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

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

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Index COPYRIGHTED MATERIAL

Chapter 5 Conditional and Iterative Statements (Part-II) To carry out repetitive task, python provides following iterative/looping statements:

Final Exam CS 251, Intermediate Programming December 10, 2014

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Questions Answer Key Questions Answer Key Questions Answer Key

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

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

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

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

Generics method and class definitions which involve type parameters.

Rules and syntax for inheritance. The boring stuff

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

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

Oracle. Java SE 8 Programmer I. 1z Version: Demo. [ Total Questions: 10] Web:

Selected Questions from by Nageshwara Rao

ECE 122. Engineering Problem Solving with Java

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

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

1 Shyam sir JAVA Notes

Examination Questions Midterm 1

CS111: PROGRAMMING LANGUAGE II

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

An overview of Java, Data types and variables

C12a: The Object Superclass and Selected Methods

Do not turn to the next page until the start of the exam.

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

CS 2340 Objects and Design - Scala

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Last class. -More on polymorphism -super -Introduction to interfaces

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

CMSC 132: Object-Oriented Programming II

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

CS 251 Intermediate Programming Inheritance

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Points To Remember for SCJP

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Final Exam Practice Questions

Vendor: Oracle. Exam Code: 1Z Exam Name: Java Certified Programmer. Version: Demo

CS 177 Week 15 Recitation Slides. Review

ECE 122. Engineering Problem Solving with Java

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Csci 102: Sample Exam

Interfaces & Generics

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

NATIONAL UNIVERSITY OF SINGAPORE

Computer Science II (20082) Week 1: Review and Inheritance

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS159 Midterm #1 Review

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Objects and Iterators

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

Java Review: Objects

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

FORM 2 (Please put your name and form # on the scantron!!!!)

A+ Computer Science -

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Transcription:

CSC 1351 The Twelve Hour Exam From Hell Name: 1 Arrays (Ch. 6) 1.1 public class L { int [] data ; void append ( int n) { int [] newdata = new int [ data. length +1]; for ( int i =0;i< data. length ;i ++) newdata [i] = data [i]; newdata [ data. length ] = n; data = newdata ; L l = new L (); l. append (3); System. out. println ( Arrays. tostring (l. data )); NullPointerException 1.2 public class L { int [] data = new int [1]; void append ( int n) { int [] newdata = new int [ data. length +1]; for ( int i =0;i< data. length ;i ++) newdata [i] = data [i]; newdata [ data. length ] = n; data = newdata ; L l = new L (); l. append (3); System. out. println ( Arrays. tostring (l. data )); [0, 3] 1.3 public class L { int [] data = new int [0]; void append ( int n) { int [] newdata = new int [ data. length +10]; for ( int i =0;i< data. length ;i ++) newdata [i] = data [i]; newdata [ data. length ] = n; data = newdata ; 1

L l = new L (); l. append (3); System. out. println ( Arrays. tostring (l. data )); [3, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1.4 public class Sum { int [] data = new int []{1,5,3,9,2,4; int sum = 0; for ( int i =0;i< data. length -1; i ++) sum += data [i]; System. out. println (" sum ="+ sum ); compile error: Sum. java :6: error : non - static variable data cannot be referenced from a static context for ( int i =0;i< data. length -1; i ++) Sum. java :7: error : non - static variable data cannot be referenced from a static context sum += data [i]; 2 errors 1.5 public class Sum { int [] data = new int []{1,5,3,9,2,4; int sum = 0; for ( int i =0;i< data. length -1; i ++) sum += data [i]; System. out. println (" sum ="+ sum ); sum=20 1.6 What could go wrong with this code? Compute all primes. static List < Integer > primeslessthann ( int n) { List < Integer > primes = new ArrayList < >(); primes. add (2); for ( int i =3;i<n;i +=2) { boolean isprime = true ; for ( Integer p : primes ) { if(i % p == 0) { isprime = false ; break ; if( isprime )

primes. add (i); return primes ; It does not work for n <= 2 1.7 Write a program to compute an List of Integers containing all the digits in an integer N. static List < Integer > digits ( int N) { List < Integer > d = new ArrayList < >(); while ( true ) { d. add (N % 10) ; N /= 10; if(n == 0) break ; return d; 1.8 Rerite the above program using arrays. 1.9 static int [] digits ( int N) { int M = N; int size = 0; while ( true ) { size ++; N /= 10; if(n == 0) break ; N = M; int pos = 0; int [] res = new int [ size ]; while ( true ) { res [ pos ++] = N % 10; N /= 10; if(n == 0) break ; return res ; Does this code compile? If so what is its output? public class Buf { int [] data ; public Buf ( int n) { data = new int [n]; int pos = 0; public void push ( int v) { data [ pos ++ % data. length ] = v; public String tostring () { return Arrays. tostring ( data ); Buf b = new Buf (3); for ( int i =0;i <10; i ++) b. push (i); System. out. println ("b="+b); b=[9, 7, 8]

1.10 Does this code compile? If so what is its output? public class Sq { List < Integer > li = new ArrayList < >(); for ( int i =0;i <3; i ++) for ( int j =0;j <=i;j ++) li.add (j +1); System. out. println (li ); [1, 1, 2, 1, 2, 3] 1.11 Does this code compile? If so what is its output? public class Sq { List < Integer > li = new ArrayList < >(); for ( int i =0;i <3; i ++) for ( int j=i;j >=0;j - -) li.add (j*j); System. out. println (li ); [0, 1, 0, 4, 1, 0] 1.12 Does this code compile? If so what is its output? public class Sq { String [] s = new String []{ "a","b","c","d"; String b = new String ("b"); for ( int i =0;i<s. length ;i ++) if(s[i] == b) System. out. print ("-B-"); else System. out. print (s[i ]); System. out. println (); abcd 1.13 Does this code compile? If so what is its output? public class Sq { String [] s = new String []{ "a","b","c","d"; String b = new String ("b"); for ( int i =0;i<s. length ;i ++) if(s[i]. equals (b)) System. out. print ("-B-"); else System. out. print (s[i ]); System. out. println ();

a-b-cd 1.14 Create a function named lsq that takes an argument n. The function should return an array of type int containing all the squares that are less than or equal to n. Thus, lsq(25) should return [1,4,16,25]. public static int [] lsq ( int n) { int sz = 0; for ( int i =0; i*i <= n;i ++) sz ++; int [] data = new int [ sz ]; for ( int i =0; i*i <= n;i ++) data [i] = i*i; return data ; 2 Classes and Inheritance (Ch. 8/9) 2.1 public void foo () { System. out. println (" Hello A"); public void foo () { System. out. println (" Hello B"); Hello B 2.2 public void foo () { System. out. println (" Hello A"); public class B implements A { public void foo () { System. out. println (" Hello B"); compile error: B. java :5: error : interface expected here public class B implements A { B. java :8: error : incompatible types : B cannot be converted to A A a = new B(); 2 errors

2.3 public void foo () { System. out. println (" Hello A"); private void foo () { System. out. println (" Hello B"); compile error: B. java :6: error : foo () in B cannot override foo () in A private void foo () { System. out. println (" Hello B"); attempting to assign weaker access privileges ; was public 1 error 2.4 interface A { public void foo () { System. out. println (" Hello A"); public class B implements A { public void foo () { System. out. println (" Hello B"); compile error: B. java :3: error : interface abstract methods cannot have body public void foo () { System. out. println (" Hello A"); 1 error 2.5 interface A { public void foo (); public class B implements A { public void foo () { System. out. println (" Hello B"); Hello B

2.6 public class B { static int a; int b; public String tostring () { return "("+a+","+b+")"; B b = new B (); b.a = 1; b.b = 2; B c = new B (); c.a = 3; c.b = 4; System. out. println (b+" "+c); (3,2) (3,4) 2.7 static void foo () { System. out. println (" Hello A"); static void foo () { System. out. println (" Hello B"); A a = new A (); Hello A 2.8 System. out. println (" works "); works 2.9 B a = new A (); System. out. println (" works "); compile error:

B. java :5: error : incompatible types : A cannot be converted to B B a = new A(); 1 error 2.10 B a = (B) new A (); System. out. println (" works "); ClassCastException 2.11 B b = (B)a; System. out. println (" works "); works 2.12 int val1 () { return 3; int val2 () { return 4; int val1 () { return 5; A a1 = new B (); A a2 = new A (); System. out. println (" Num ="+( a1. val1 ()* a2. val2 ())); Num=20 2.13 boolean issuper () { return true ; void show () { if( issuper ()) System. out. println (" Super!"); else System. out. println (" Not super.");

boolean issuper () { return false ; A a = new A (); a. show (); a = new B (); a. show (); Super! Not super. 2.14 boolean issuper () { return true ; boolean issuper () { return false ; A a1 = new A (); A a2 = new B (); B b = null ; if (! a1. issuper ()) b = (B)a1; if (! a2. issuper ()) b = (B)a2; System. out. println (" Found B"); Found B 2.15 boolean issuper () { return true ; boolean issuper () { return false ; A a1 = new A (); A a2 = new B (); B b = null ; if(a1. issuper ()) b = (B)a1; if(a2. issuper ()) b = (B)a2; System. out. println (" Found B"); ClassCastException 2.16 A a1 = new A (); A a2 = new B (); B b = null ; if(a1 instanceof B) b = (B)a1;

if(a2 instanceof B) b = (B)a2; System. out. println (" Found B"); Found B 3 Anonymous Inner Classes (Covered with Ch. 8/9) The java.lang package has a class called Runnable. package java. lang ; public interface Runnable { public void run (); 3.1 Write a complete Hello, World program in Java that instantiates an anonymous inner class based on the Runnable interface, then calls the instantiated object to print the message. public class HelloRun { Runnable r = new Runnable () { public void run () { System. out. println (" Hello, World "); ; r. run (); 3.2 Rewrite the following code to make use of an anonymous inner class class MyAdapater extends MouseAdapter { public void mousepressed ( MouseEvent me) { System. out. println (" Press ");... public class MyApp { JFrame bf = new JFrame (" MyApp "); bf. addmouselistener ( new MyAdapter ()); public class MyApp { JFrame bf = new JFrame (" MyApp "); bf. addmouselistener ( new MouseAdapter () { public void mousepressed ( MouseEvent me) { System. out. println (" Press "); );