Task 2 What is printed out when the code is executed?

Similar documents
Introduction to Linked Data Structures

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

DNHI Homework 3 Solutions List, Stacs and Queues

CMP-338 Solutions Midterm Spring Version 1

Suppose that we have linked list of integers where each node is represented as: // An item in the list.

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

King Saud University College of Computer and Information Systems Department of Computer Science CSC 113: Java Programming-II, Spring 2016

Arrays. Chapter Arrays What is an Array?

Name Section Number. CS210 Exam #1 *** TURN OFF ALL ELECTRONIC DEVICES *** Practice

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

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

ADTs, Arrays, Linked Lists

1. ArrayList and Iterator in Java

ITI Introduction to Computing II

CMPSCI 187 Midterm 1 (Feb 17, 2016)

Linked Lists. A dynamically resizable efficient list implementation. Tip For Success: Reminder

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Programming I Part 3 Algorithms and Data Structures

Oracle 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

/department of mathematics and computer science 1/58

CSE 143 SAMPLE MIDTERM

CS2012 Programming Techniques II

CS18000: Problem Solving and Object-Oriented Programming

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Lists. ordered lists unordered lists indexed lists 9-3

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Recursive Objects. Singly Linked List (Part 2)

Java Review: Objects

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Chapter 5 - A Simple Dynamic Linked List (Holding Strings)

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Search,Sort,Recursion

Distributed Systems Recitation 1. Tamim Jabban

COP 3337 Test 3. private int minimumposition(int from) { Part 1: Selection Sort

C/C++ Programming Lecture 18 Name:

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Arrays.

Distributed Systems Recitation 1. Tamim Jabban

Advanced Computer Programming

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Linked Lists. A dynamically resizable, efficient implementation of a list. Tip For Success: Reminder

Memorandum public override string string Node while null return public void bool false Node while null null true while public void Node new

Final Exam. COMP Summer I June 26, points

Java Standard Edition 6 Programmer Certified Professional Exam

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CS 1322 Test 2 - Version 1 Tuesday June 24 th

: Advanced Programming Final Exam Summer 2008 June 27, 2008

CIS 110 Introduction to Computer Programming Summer 2016 Final. Recitation # (e.g., 201):

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

Do not open this examination paper until instructed to do so. Answer all the questions.

CIS 110 Introduction to Computer Programming Summer 2016 Final. Recitation # (e.g., 201):

LinkedList Implementation Mini-project intro

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Searching & Sorting in Java Bubble Sort

Sun Certified Programmer for the Java 2 Platform. SE6.0

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Object Oriented Programming. Java-Lecture 6 - Arrays

SUN Upgrade for the Sun Certified Java Programmer. SE 6.0. Download Full Version :

The code XXX1XXX, XXX2XXX, XXX3XXX represents pieces of code that are missing from the method that are needed to make it work properly.

Object-Oriented Programming with Java

CS 302 Week 9. Jim Williams

Midterm Spring 2015 Solutions Version 1

Pace University. Fundamental Concepts of CS121 1

Complexity, General. Standard approach: count the number of primitive operations executed.

ITI Introduction to Computing II

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Engi Hafez Seliem

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

1 Constructors and Inheritance (4 minutes, 2 points)

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CSE 143 SAMPLE MIDTERM SOLUTION

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Final. Please write (printing clearly) the infonnation requested in the boxes at the top of this page.

Week 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Linked List Nodes (reminder)

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

CS 314 Exam 2 Spring

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

Sort: Divide & Conquer. Data Structures and Algorithms Emory University Jinho D. Choi

Testpassport. T e s tpas s port.

CSC 321: Data Structures. Fall 2017

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

CS24 Week 4 Lecture 2

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

16. Dynamic Data Structures

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Java Memory Management

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

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Transcription:

Task 1 What is printed out when the code is executed? public class Class1 { public static void main(string[] args) { int array[] = {14,5,7; for (int counter1 = 0; counter1 < array.length; counter1++) { for (int counter2 = counter1; counter2 > 0; counter2- - ) { if (array[counter2-1] > array[counter2]) { int variable1 = array[counter2]; array[counter2] = array[counter2-1]; array[counter2-1] = variable1; for (int counter3 = 0; counter3 < array.length; counter3++) System.out.println(array[counter3]); 5, 7, 14 (insertion sort) Task 2 What is printed out when the code is executed? public class Class2 { public static void main (String args[]){ int [] variable1 = new int[128]; Class3 variable2 = new Class3(variable1, 0); variable2.method1(7); variable2.method1(2); variable2.method1(8); System.out.println(variable2.method2()); System.out.println(variable2.method2()); System.out.println(variable2.method2()); public class Class3 { private int [] array; private int index; public Class3(int[] array, int index) { this.array = array; this.index = index; public void method1 (int variable3) { array[index] = variable3; index++; public int method2 () {

index- - ; return array[index]; public int methode3(int index) { return array[index]; 8, 2, 7 (Class3 implements a Stack. Numbers are pushed and then popped.) Task 3 The data structure in Classe3 implements a Stack. The implementation is very simple and can lead to problems. a) Which poblems does that implementation have? public class Stack { private int [] elements; private int index; public Class3(int[] elements, int index) { this.elements = elements; this.index = index; public void push (int item) { elements[index] = item; index++; public int pop () { index- - ; return elements[index]; public int top () { return elements[index]; b) How would you improve the implementation? (verbal description suffices; no need to write source code). a) No checks whether stack is full or empty; passed index is not checked Furthermore, a list would be better than an array. b) Include security checks; use list instead of array. Task 4 The source code shows a linked list. What is printed out when the code is executed? public class AbstractElement implements Comparable<Object> {

private String name; private int attribute; public AbstractElement(String n) { this.name = n; public AbstractElement(String name, int attr) { this.attribute = attr; this.name = name; public int compareto(object o1) { return this.name.compareto(((abstractelement) o1).name); public String tostring() { return this.name; public String getname() { return name; public void setname(string name) { this.name = name; public int getattribute() { return attribute; public void setattribute(int attribute) { this.attribute = attribute; public class LinkedList { Node head; public LinkedList() { this.head = new Node(); public Node getelementat(int pos) { Node current = this.head; for (int i = 0; i <= pos; i++) { if (current.getnext()!= null) { current = current.getnext(); else { return null; return current; public void insert(abstractelement e) { Node newnode = new Node(e); newnode.setnext(this.head.getnext()); head.setnext(newnode); public void insertalgorithm(abstractelement ae){ Iterator iter = this.iterator(); Node previous = iter.next(); if (ae.compareto(previous.getelement()) < 0) { insert(ae); return; while (iter.hasnext()) { Node tmp = iter.next(); if (ae.compareto(tmp.getelement()) < 0) { Node newnode = new Node(ae); newnode.setnext(tmp); previous.setnext(newnode); break; previous = tmp;

if (ae.compareto(previous.getelement()) > 0) { Node newnode = new Node(ae); previous.setnext(newnode); return; public String removepos(int pos) { Node previous = null; Node current = this.head; Node delete = null; for (int i = 1; i <= pos; i++) { previous = current; if (current.getnext()!= null) { current = current.getnext(); else { return null; delete = current; previous.setnext(current.getnext()); return delete.getelement().getname(); public int size() { Node iterator = head; int size = 0; while (iterator.getnext()!= null) { iterator = iterator.getnext(); size++; return size; public final void sortbubblesort() { AbstractElement[] array = new AbstractElement[this.size()]; Node current = this.head; int i = 0; array[i] = current.getelement(); while ((current!= null) && ((current = current.getnext())!= null)) { array[i++] = current.getelement(); sort(array, this.size()); i = 0; current = head; while ((current!= null) && ((current = current.getnext())!= null)) { current.setelement(array[i++]); public final void sortperformance() { long t1 = System.currentTimeMillis(); sortbubblesort(); long t2 = System.currentTimeMillis(); t2 = t2 - t1; System.out.println("Sortierung benˆtigte: " + t2 + " Milisekunden"); public void sort() { Runtime r = Runtime.getRuntime(); long mem1, mem2; mem1 = r.totalmemory(); mem2 = r.freememory(); sortperformance(); mem1 = r.totalmemory() - mem1; mem2 = r.freememory() - mem2; System.out.println("Verwendeter Speicher: " + mem1 + " ; ƒnderung der Speicherauslastung: " + mem2); private void sort(abstractelement[] array, int size) { boolean swapped; do {

swapped = false; for (int i = 0; i < size - 1; i++) { if (array[i + 1].compareTo(array[i]) < 0) { swap(array, i, i + 1); swapped = true; while (swapped); private void swap(abstractelement[] array, int index1, int index2) { AbstractElement swaptmp = array[index1]; array[index1] = array[index2]; array[index2] = swaptmp; public String tostring() { String result = ""; Iterator it = this.iterator(); while (it.hasnext()) { result += it.next().tostring() + "\r\n"; return result; public Iterator iterator(){ return new Iterator(this, 0); public class Node { private AbstractElement element; private Node next; public Node() { this.element = null; this.next = null; public Node(AbstractElement e) { this.element = e; this.next = null; public AbstractElement getelement() { return this.element; public void setnext(node n) { this.next = n; public Node getnext() { return this.next; public void setelement(abstractelement e) { this.element = e; public class ListMain { public static void main(string args[]) { LinkedList list = new LinkedList(); AbstractElement a1 = new AbstractElement("A1"); AbstractElement a2 = new AbstractElement("A2"); AbstractElement a3 = new AbstractElement("A3"); AbstractElement a4 = new AbstractElement("A4"); AbstractElement a5 = new AbstractElement("A5"); list.insert(a5); list.insert(a4); list.insert(a3); list.insert(a2); list.insert(a1);

System.out.println(list.remove(0)); A NullPointerException, because in the method remove, a!= null check is missing. The loop starts with 1, but is called with 0. Task 5 The source code shows a linked list. What is printed out when the code is executed? (Line 1 to 15 are the same as in the last task)? public static void main(string args[]) { LinkedList list = new LinkedList(); AbstractElement a1 = new AbstractElement("A1"); AbstractElement a2 = new AbstractElement("A2"); AbstractElement a3 = new AbstractElement("A3"); AbstractElement a4 = new AbstractElement("A4"); AbstractElement a5 = new AbstractElement("A5"); list.insert(a5); list.insert(a4); list.insert(a3); list.insert(a2); list.insert(a1); Iterator iter = list.iterator(); while (iter.hasnext()) { System.out.println(iter.next().getElement()); First A1, then A5; method insert inserts elements at the front of the list. Task 6 What is printed out, if the method is extended as shown? (Line 1 to 20 are the same as for the last task) public static void main(string args[]) { LinkedList list = new LinkedList(); AbstractElement a1 = new AbstractElement("A1"); AbstractElement a2 = new AbstractElement("A2"); AbstractElement a3 = new AbstractElement("A3"); AbstractElement a4 = new AbstractElement("A4"); AbstractElement a5 = new AbstractElement("A5"); list.insert(a5); list.insert(a4); list.insert(a3); list.insert(a2); list.insert(a1); Iterator iter = list.iterator();

while (iter.hasnext()) { System.out.println(iter.next().getElement()); list.insertalgorithm(new AbstractElement("A6")); iter = list.iterator(); while (iter.hasnext()) { System.out.println(iter.next().getElement()); Lösung: Answer for Task 5 + A6; insertalgorithm inserts elements in lists sorted in ascending order. Task 7 The source code shows a linked list. What is printed out when the code is executed? (Line 11 to 15 insert elements in another order; A0 is added). public static void main(string args[]) { LinkedList list = new LinkedList(); AbstractElement a1 = new AbstractElement("A1"); AbstractElement a2 = new AbstractElement("A2"); AbstractElement a3 = new AbstractElement("A3"); AbstractElement a4 = new AbstractElement("A4"); AbstractElement a5 = new AbstractElement("A5"); list.insert(a1); list.insert(a2); list.insert(a3); list.insert(a4); list.insert(a5); list.insertalgorithm(new AbstractElement("A0")); iter = list.iterator(); while (iter.hasnext()) { System.out.println(iter.next().getElement()); Inserting sorted works only with lists sorted in ascending order. Thus, the answer is: A5, A0, A4, A3, A2, A1 Task 8 Implement a method to insert elements in a list sorted in descending order. Example Sorted list: A6 A4 A3 A2 A1 When A5 is inserted, the order should be kept: A6 A5 A4 A3 A2 A1

Use insertalgorithm and change the comparisons; i.e., > changes to <, and < changes to >. Another way is to call insert and then adjust and use method bubblesort. Task 9 What is printed out when the code is executed? public class Test public static void main(string[] args){ String ausgabe = null; if(args[0].equals("hello")) { ausgabe = args[0] + " World"; System.out.println(ausgabe); Depending on whether "Hello" is passed as command- line parameter: Yes: Hello World No: NullPointerException Task 10 You see a program for the manipulation of multi- media data on mobile devices. You can manage photos in albums, set photos as favorites, and sort photos. However, when you add a picture to an album, a NullPointerException is thrown. What is the problem? Variable photopathtxt in class AddPhotoToAlbum is initialized with null.