CSC 1302: Final Exam Study Guide
The child class in an inheritance relationship is called ___________.
i. Subclass ii. Superclass iii. Interface iv. Overloading - i.
Listener
an object that "waits" for an event to occur and responds in way when it does
Component
an object that defines a screen element used to display information or allow the user to interact with the program
Consider the following method: public static void arrayListMystery (ArrayList<Integer> list){ for (int i = 0; i < list.size() - 1; i++){ if (list.get(i) > list.get(i + 1)) { list.set(i + 1, list.get(i + 1) * 2); } } } Indicate what will be the new list after arrayListMystery. (a) list1 = [10] arrayListMystery(list1); (b) list2 = [0, 7, 16, 3, -5] arrayListMystery(list2); Giving the following method, write the output when the following method is passed the following list: public static void mystery2 (ArrayList<Integer> list){ for(int i = 0; i < list.size() -1; i++){ int element = list.get(i); list.remove(i); list.add(i + 1, element + 2); } System.out.println(list); } [5, 40, 3, 20, 1]
(a) 10 0 is not < 0 (b) 1, 7, 16, 6, -10 (c) [40, 3, 20, 1, 13]
Consider the following method: public static void main arrayMystery(int [ ] a) { for (int i = 0; i < a.length-1; i ++) { a [ i ] = i + a [i + 1] - a [ i ]; } } Indicate what will be the values stored in the array after the values pass through the arrayMystery( ); (a) int [ ] a1 = {5} arrayMystery(a1); (b) int [ ] a2 = {3, 13, 6} arrayMystery(a2); (c) int [ ] a3 = {-2, 8, -1, 6, 2, 8} arrayMystery(a3);
(a) 5 (b) 10, -6, 6 (c) 10, -8, 9, -1, 10, 8
(a) After a single pass of selection sort algorithm (a single swap), what would be the state of the array? [17, 16, 122, 3, -3, 17, 0, -2, 31, 1] (b) After a single pass of bubble sort algorithm(could be zero/one/many swaps), what would be the state of the array? [17, 16, 122, 3, -3, 17, 0, -2, 31, 1]
(a) [-3, 16, 122, 3, 17, 17, 0, -2, 31, 1] (b)[16, 17, 3, -3, 17, 0, -2, 31, 1, 122]
(a)Given the class NewClass below, what is wrong with the following constructor? Point out the error and fix it. public class Employee{ private int id; private String name; } public void Employee(int intid, String name){ int id = intid; name = this.name; } (b) What is wrong with the following interface? Point out the error and fix it. public class anInterface{ public int aMethod(int a ); }
(a) public Employee (int id, String name){ this.id = id; this.name = name; } (b) public interface anInterface{ public int aMethod(int a); }
(a)What indexes will be examined by a sequential search for the target value 16? [-5, 5, 10, 15, 16, 22, 34, 52, 52, 53, 79] (b) What indexes will be examined as the middle elements by a binary search for the target value 3? [-5, 5, 10, 15, 16, 22, 34, 52, 52, 53, 79]
(a)0, 1, 2, 3, 4 (b)5, 2, 1, 0
public class Date { int month; int day; } (a) Given the Date class above, write a toString( ) method for the above class. For example if d1.month = 6 and d1.day = 15 then System.out.println(d1) should print "Day: 15, Month: 6". (b) Given the Date class above what are the month and day of the Dates referred to as d1 and d2 after the following code executes? Give your answer as month, day Date d1 = new date ( ); Date d2 = new date ( ); d1.day = 15; d2.month = 10; d2.day = 5; d1.month = 2;
(a)public String toString( ){ return "Day: " + day + "Month: " + month } (b) d1 = Month: 2, Day: 15 d2 = Month: 10, Day: 5
Complete the following House class below. Also add the compareTo( ) method that would order the objects according to the city and if they are in the same city then they are ordered according to the zipcode and if the zipcodes are the same then use the streetAddress. public class House implements Comparable <House> { private String city; private int zipcode; private String streetAddress; //Constructor that constructs the object public House { //the accessors or getters public String getCity( ){ public int getZipcode( ){ public String getStreetAddress( ){ //returns the address with the street address, city then zipcode EX: 25 Park PL NE, Atlanta, 30303 public String toString( ){ //your compareTo( )method goes here public int compareTo(House other){
- (String city, int zipcode, String streetAddress) this.city = city; this.zipcode = zipcode; this.streetAddress = streetAddress; - return city; - return zipcode; - return streetAddress - return streetAddress + " , " + city + " , " + zipcode; - if (city.compareTo (other.city) != 0){ return city; }else{ if (zipcode != zipcode){ return zipcode - other.zipcode; }else{ return streetAddress; }
Which is the following is a protocol for controlling the right to transmit in a network? A. UDP B. CSMA/CD C. TCP D. FTP
- B
Extending adapter class
- each listener interface that contains more than one method has corresponding adapter class containing empty definitions for all methods in the interface
To create a Java program that uses a GUI, we must:
- instantiate and set up the necessary components - implement listener classes that define what happens when particular event occur, and - establish the relationship between the listeners and the components that generate the events of interest
ActionListener interface
- only method listed is the actionPerformed method - the button component generates the action event resulting in a call to actionPerformed method, passing an ActionEvent object
In addition to push button, there are variety of other interactive components
- text fields - check boxes - radio buttons - sliders - combo boxes - timers
Containment hiearchies
- the way components are grouped into containers, and the way those containers are nested within each other - for any Java GUI program, there is generally one primary (top-level) container, such as frame or applet - the top-level container often contains one or more containers, such as panels - these panels may contain other panels to organize the other components as desired
Frames and Panels are classified as either:
-heavyweight: managed by the underlying operating system - EX: a frame, an applet -lightweight: managed by the Java program - EX: a panel, more complex than heavyweight containers
Complete the following method that performs binary search. public static int binarySearch( int [ ]a, int target) { int min = __; int max = __; while(min <= max){ int mid = __; if(a [mid] < target) { min = ___; }else if (a[mid] > target){ max = __; }else{ return ___; //target found } } return -(min + 1); //target }
0 a.length-1; (max + min)/2 mid + 1 mid - 1 mid
What indexes will be examined as the middle element by a binary search for the target value 6? [5, 7, 12, 15, 19, 27, 33, 39, 50, 52, 55, 70]
0, 1, 2, 5
Three kinds of objects are needed to create a GUI in Java:
1. components 2. events 3. listeners - In designing a GUI-based program we need to establish the relationships between the listener, the event it listens for, and the component that generates the event
Java components and other GUI-related classes are defined primarily in two packages:
1. java.awt - the Abstract Window Toolkit (AWT) was the original Java GUI package - contains many important classes we will use 2. javax.swing - the Swing package was added later - provides components that are more versatile than those of AWT
import java.util.*; public class Test1{ public static void main (String [ ] args) { int y = 1; int x = 3; ArrayList<Integer> a = new ArrayList<Integer>(); a.add(0); a.add(1); a.add(2); a.add(3); mystery(a, y, x); System.out.println(x + " " + y + " " + a); x = y - 1; mystery(a, y, x); System.out.println(x + " " + y + " " + a); } public static void mystery (ArrayList<Integer> a, int x, int y){ if( x < y) { x++; a.set(x, 17); }else{ a.set(y, 17); } System.out.println(x + " " + y + " " + a); }
2 3 [0, 1, 17, 3] 3 1 [0, 1, 17, 3] 1 0 [17, 1, 17, 3] 0 1 [17, 1, 17, 3]
Write the output of the following program below, as it would appear on the console. public class Recur{ public static void main (String [ ] args){ print(5); } public static void print (int n) { if (n != 0){ print (n - 1); System.out.print (n + n); } } }
5 != 0 4 != 0 3 != 0 2 != 0 1 != 0 0 = 0 1 + 1 = 2 2 + 2 = 4 3 + 3 = 6 4 + 4 = 8 5 + 5 = 10 2, 4, 6, 8, 10 - prints in reverse
Which layer of the TCP/IP hierarchy chops messages into units whose size is compatible with the internet?
A. Application B. Transport C. Network D. Link - B
Which of the following connects existing networks to form an internet?
A. Bridge B. Router C. Switch D. Repeater - B
Which of the following provides large scale streaming services?
A. CGI B. ICANN C. CDN D. PAN - C
Which of the following is not a means of implementing server-side activities?
A. CGI B. JSP C. ASP D. JavaScript - D
Which of the following is not a means of performing interprocess communication over a network?
A. Client/server B. ICANN C. Peer-to-peer - B
Which of the following is not an application of the Internet?
A. FTP B. Email C. Telnet D. CERT -D
Which of the following is used to translate between IP addresses and mnemonic address?
A. File server B. Mail server C. Name server D. FTP server - C
The primary purpose of which of the following is not an application of the Internet?
A. ICANN B. Firewall C. Encryption - A
Which of the following is not protocol used in the basic TCP/IP software hierarchy?
A. POP3 B. UDP C. TCP D. IP - A
CSMA/CA is a means of implementing which of the following network topologies?
A. Star B. WiFi C.. Bus -Star or WiFi (mostly star)
Which of the following is not a means of connecting networks?
A. Switch B. Server C. Router D. Bridge - B
Which of the following is assigned the task of providing individual users access to the Internet?
A. Tier-1 ISPs B. Tier-2 ISPs C.Access ISPs D. ICANN - C
Which of the following is not a way of classifying networks?
A. WAN versus LAN B. Closed versus open C. Router versus bridge D. Star versus bus - C
List two network topologies.
A. bus B. star
public class Tupac { public void a( ){ System.out.print("Tupac a "); } public void b( ){ System.out.print("Tupac b "); } public String toString( ) { return "Tupac"; } } public class JayZ extends Tupac { public void a( ){ System.out.print("JayZ a "); b( ); } } public class Biggie extends JayZ { public void a( ){ System.out.print("Biggie a "); super.a( ); } public String toString( ){ return "Biggie"; } } public class FiftyCent extends Biggie { public void b( ){ System.out.print("FiftyCent b "); } } Given the classes above, what output is produced by the following code? Tupac [ ] elements = {new Biggie( ), new Tupac ( ), new JayZ( ), new FiftyCent( )}; for(int i = 0; i < elements.length; i++) { elements[i].a( ); System.out.println( ); elements[i].b( ); System.out.println; System.out.println(elements[i]); System.out.println; }
Biggie a JayZ a Tupac b Tupac b Biggie Tupac a Tupac b Tupac JayZ a Tupac b Tupac b Tupac Biggie a JayZ a FiftyCent b FiftyCent b Biggie
Which of the following concepts is not associated with critical regions?
Bootstrap
Which of the following is a task that not performed by the kernel of an operating system?
Communicate with the user
Event
an object that represents some occurrence in which we may be interested - often corresponds to user actions (mouse button press, keyboard key press) - most GUI components generate events to indicate a user action related to that component
_______can represent a common super type beteween several classes with code sharing
Inheritance
Which of the following is not an attempt to provide security?
Mutitasking
The complexity of binary search algorithm is
O(log n)
The complexity of Bubble Sort algorithm is
O(n^2)
Assume that the following four class have been defined: public class Shapes { public void method1 ( ){ System.out.print("S1 "); } public void method2( ){ System.out.print("S2 "); } public String toString( ){ return "All Shapes!"; } } public class Circle extends Shapes { public void method2( ) { System.out.print("C2 "); super.method2( ); } } public class Rectangle extends Shapes { public void method1( ){ System.out.print("R1 "); } public String toString( ){ return "Rectangle!"; } } public class Square extends Rectangle { public void method1( ){ System.out.print("SQ1 "); } public void method2( ){ System.out.orint("SQ2 "); }
SQ2 Rectangle! SQ1 C2 S2 S1 All Shapes! S1 S2 R1 Rectangle! R1 S2 S1 All Shapes! S1
Which of the following items of information would not be contained in an operating system's process table?
The machine language instructions being executed by the process
After single pass of selection sort algorithm (a single swap), what would be the state of the array? [7, 6, 11, 13, -6, 17, 10, 23, -1, 1]
[-6, 6, 11, 12, 7, 17, 10, 23, -1, 1]
After a single pass of bubble sort algorithm (could be zero/one/many swaps), what would be the state of the array? [7, 6, 11, 13, 6, 17, 10, 23, -1, 1]
[6,7, 11, 6, 13, 10, 17, -1, 1, 23]
pack method
____ of the frame sets the frame size accordingly
inner class
a class defined inside another class - have access to the members of the class that contains it
Dialog box
a graphical window that pops up on top of any currently active window so that the user can interact with it - conveys information - confirms an action - permits the user to enter information
Container
a special type of component that is used to hold and organize other components
Write a method that takes an array of integers as a parameter and sorts the array using either one of the following algorithms. (Just write one method) a. Selection Sort algorithm(called selectionSort( )) b. Bubble sort algorithm(called bubbleSort( ))
a. public static int[ ] selectionSort(int [ ]a){ for(int i = 0; i <a.length - 1; i++){ int min = i; for(int j = i + 1 <a.length; j++){ if(a [min] < a[j]){ min = j; } swap(a, min, j); } public static void swap(int [ ]a, a1, a2){ int temp = a[a1]; a[a1] = a[a2] a[a2] = temp; }
public class StaticEx{ private StaticEx( ) { count++; } public static int getCount( ){ return count; } } public class Client { public static void main(String [ ] args){ StaticEx s1 = new StaticEx( ); //point 1 StaticEx s2 = new StaticEx( ); //point 2 } } Given the StaticEx class and Client class above, what will be the values returned when the following calls are made at the given location. a. StaticEx.getCount( ) (at point 1) b. s1.getCount( ) (at point 2)
a. 4 - (3 + 1) b. 5 - (4 + 1)
If the objects of a class are arranged into their natural order, then if object L is left of object R what is true?
a. L.compareTo(R) is negative b. L.compareTo(R) is positive c. L.compareTo(R) is zero d. R.compareTo(L) is postive - a and d
Say that you are writing a class Pen and wish it to implement the Comparable<Pen> interface. What method must Pen contain?
a. int compareTo (Pen other) b. int compareTo (Object other) c. Pen compareTo (Object other d. boolean compareTo (T other) - a
What is the value of of "zebra".compareTo("lion")?
a. negative integer b. 0 c. a positive integer d. unpredictable - c
What indexes will be examined by a sequential search for the target value 6? [5, 7, 12, 15, 19, 27, 33, 39, 50, 52, 55, 70]
all of the indexes will be examined
Sliders
allow the user to specify a numeric value within a bounded range - can be presented either vertically or horizontally - produces a change event, indicating that the position of the slider and the value it represents has changes - defined by the JSlider class
The panel's add method:
allows a component to be added to the panel
Combo boxes
allows a user to select one of several options from a "drop down" menu - when the user presses a combo box using a mouse, a list of options is displayed from which the user can choose - defined by the JComboBox class - generate an action event whenever the user makes a selection from it
push button
allows the user to initiate with a press of the mouse - generates an action event - different components generate different types of events
Write a statement to create an array of the above Date class with 3 elements in it. Initialize the array to store following dates: Day: 15, Month: 6 Day: 12, Month: 11 Day: 5, Month: 4
date [ ] a = new Date [3] a [0] = new Date [ ]; a [0] day = 15; a [0] month = 6; a [1] = new Date [ ]; a [1] day = 12; a [1] month = 11; a [2] = new Date [ ]; a [2] day = 5; a [2] month = 4;
setDefaultCloseOperation method:
determine what will happen when the close button in the corner of the frame is clicked
label
displays a line of text - can be used to also display an image - labels are non-interactive
Timers
do not have a visual representation, but are a AWT component - defined by the Timer class and are provided to help manage activity over time - a timer object generates an action event at regular intervals
Layout mangers
every container is managed by an object known as _____ that determines how the components in the container are arranged visually - is consulted when needed, such as when the container is resized or when a component is added - every container has a default layout manager, but can be replaced if desired -determines the size and position of each component - every layout manager has its own rules and properties governing the layout of the components it contains - for some, the order in which you add the components affects their positioning - use the setLayout method of a container to change its layout manager
Complete the following method that scales all the elements in the ArrayList by 3. public static void scaleBy3(ArrayList<Integer> list){ for (int i = 0;
for (int i = 0; i < list.size( ); i++){ list.get(i); list.set(i, list.get(i) * 3); }
Key event
generated when the user presses a keyboard key - allows a program to respond immediately to the user while they are typing - the KeyListener interface defines three methods - keyPressed, keyReleased, keyTyped
Text fields
generates an action event when the Enter or return key is pressed (and the cursor is in the field)
Check boxes
generates an item event when it changes state from selected (checked) to deselected (unchecked) and vice versa - JCheckBox class is used to define check boxes
Objects can protect their internal data from unwanted external modification by declaring them to be private, an action known as __________.
i. Encapsulation ii. Inheritance iii. Polymorphism iv. Interface - i.
Treating objects of different types interchangeably is called __________.
i. Encapsulation ii. Inheritance iii. Polymorphism iv. Interface - iii.
__________ can represent a common super type between several classes without code sharing.
i. Encapsulation ii. Inheritance iii. Polymorphism iv. Interface - iv.
To __________ a method is to implement a new version of the method to replace code that would otherwise have been inherited from a superclass.
i. Overload ii. Override iii. Implement iv. Extend - ii.
Which necessary condition for deadlock is removed by each of the following: i. require processes to request all required resources at once ii. allow only one process at a time in the process table. iii. take all resources when deadlock occurs and restart the process
i. resources must be required on a partial basis ii. must be compatible for non-shareable resources iii. resources cannot be forcibly removed
A container is governed by a
layout manager - the default layout manger for a panel simply displays components in the order they are added, with as many components on one line as possible
Mouse events
occur when the user interacts with another component via the mouse. to use, implements MouseListener interface class
Mouse motion events
occur while the mouse is in motion. to use, implement the MouseMotionListener interface class
Flow layout
organizes components from left to right, starting new rows as necessary - JPanel class uses flow layout by default
GridBag layout
organizes components into a grid of cells, allowing components to span more than one cell
Grid layout
organizes components into a grid of rows and columns - one components is placed in each sell, and all cells are the same size - the number of rows and columns in a grid layout is established by using parameters to the constructor when the layout manager is created - as components are added to the grid layout, they fill the grid from left to right, top to bottom - there is no way to explicitly assign a component to a particular location in the grid other than the order in which they are added to the container
Box layout
organizes components into a single row or column, either vertically or horizontally - components are organized in the order in which they are added to the container - two types of invisible components can be added to control spacing between other components - rigged areas: with a fixed size, glue: specifies where excess space should go as needed
Border layout
organizes components into five area (North, South, East, West, and Center) - the four outer areas are large as needed in order to accommodate the component they contain - if no components are added to a region, the region take up no room in the overall layout - the Center area expands to fill any available space - the size of the gaps between the areas can be adjusted
Card layout
organizes components into one areas such that only one is visible at any time
Write the output of the following program below, as it would appear on the console. public class Recur{ public void main (String [ ] args){ print(5); System.out.print( ); print(6); } public static void print (int n){ if(n == 1){ System.out.print("1"); }else{ if(n%2 == 0) System.out.print(n * n + ", "); print(n - 1); }else{ print(n - 1); System.out.print(", " + n * n); } } } }
print(5): 16, 4, 1, 9, 25 print(6): 36, 16, 4, 1, 9, 25 - prints even first
Defining a Listener
private class ButtonListener implements ActionListener { ... }
Event-driven
program that is oriented around GUI, responding to user events
Suppose you have the class ClockTime below. Complete the following class. public class ClockTime { private int hour; private int minute; private String amPm; //Constructs a new time for the given hour/minute public ClockTime(int hour, int m, String ap) public int getHour() public int getMinute() public int getMinute() public String getAmPm() public String toString() public boolean equals (Object other)
public ClockTime(int hour, int m, String ap){ this.hour = hour; this.m = minute; this.amPm = ap; } public int getHour(){ return hour; } public int getMinute(){ return minute; } public String getAmPm(){ return amPm; } public String toString(){ return hour + " : " + minute + amPm; } public boolean equals (Object other){ if (other instanceOf ClockTime){ ClockTime other = (ClockTime)other; return hour == other.hour && m == other.minute && amPm.equals(other.amPm); }else{ return false; } }
Write a compareTo method for the following Cat class. The cat objects should be sorted according to name, if names are the same then according to their age. public class Student implements Comparable <Student>{ private String name; private int age; ... public int compareTo (Cat other){
public int compareTo (Cat other){ if (name.compareTo (other.name) != 0){ return name; }else{ return age - other.age; } }
Write a recursive method called cumumlativeSumOdd(n) that returns the sum of all odd number from 1 to n. (No loop should be used.)
public static int cumulativeSumOdd(int n){ if(n == 1){ return 1; }else if( n%2 == 0){ return cumulative sumOdd(n -2); }else{ return n + cumultiveSumOdd(n -2); } }
Write a method called minLength that takes an ArrayList of Strings as a parameter. It should return the length of the longest String in the list and remove the String from the list. If your method is passes an empty ArrayList, it should return 0.
public static int minLength(ArrayList<String> list){ if (int size( ) == 0){ return 0; } int max = 0; int length = 0; for(int i = 1; i <list.size( ); i++){ if(list.get(i).length( ) > list.get(max).length( ){ max = i; } } length = list.get(max).length( ); list.remove(max); return length; }
getSource method
returns a reference to the component that generated the event
public class Lamp { public void method1( ){ System.out.print("lamp 1 "); } public void method2( ){ System.out.print("lamp 2 "); } public String toString() { return "lamp"'; } } public class Sock extends Lamp { public void method1( ){ System.out.print("sock 1 "); method2(); } public String toString( ){ return "sock"; } } public class Book extends Sock { public void method2( ){ System.out.print("book 2 "); super.method2( ); } } public class Pen extends Sock { public void method1 ( ){ System.out.print ("pen 1 "); } } Given the class above, what output is produced by the following code? Lamp [ ] elements = {new Book( ), new Pen( ), new Lamp( ), new Sock ( )}; for(int i = 0; i < elements.length; i++){ System.out.println(elements [i]); elements [i].method1 ( ); System.out.println ( ); elements [i].method2 ( ); System.out.println( ); System.out.println( ); }
sock sock 1 book 2 lamp 2 book 2 lamp 2 sock pen 1 lamp 2 lamp lamp 1 lamp 2 sock sock 1 lamp 2 lamp 2
getContentPane method
the content pane of the frame is obtained using this method
add method
the content panes ____ method is used to add the panel
In the main method:
the frame for the program is constructed, set up, and displayed
Radio button
used with other radio buttons to provide a set of mutually exclusive options - have meaning only when used with one or more other radio buttons - produce an action event when selected - defined by JRadioButton class
Listener class
written by implementing an interface (list of methods the implementing class must implement) -EX: the ButtonListener implements ActionListener interface