Application Programming final review (CS 3443)
Consider the following lines of code public class Promotion { public static void main(String args[]) { int i = 5; float f = 5.5f; double d = 3.8; char c = 'a'; if (i == f) c++; if(((int) (f+d)) == ((int) f + (int) d )) c +=2; System.out.println(c); } } A. The code will fail to compile B. It will print a C. It will print b D. It will print c
B
What will the following code output? import java.util.*; import java.io.IOException; class A { public static void main(String[] args) { List<String> color = new ArrayList<String>(Arrays.asList("blue", "red", "yellow", "orange")); color.add(2, "pink"); System.out.println(color); } } A. [blue, red, pink, orange] B. [blue, red, pink, yellow, orange] C. [blue, pink, yellow, orange] D. [blue, red, yellow, pink, orange]
B
Which of the following modifiers is used as a substitute for multiple inheritance in Java? A. Abstract B. Interface C. public D. protected
B
How do you model the following situation with a UML class diagram: A fair is visited by at least one visitor. One visitor visits at least one fair. A. [Visitor] 1..* ------- * [Fair] B. [Visitor] 1 ------- 1* [Fair] C. [Visitor] 1..* ------- 1..* [Fair] D. [Visitor] * ------- * [Fair]
C
java.util.Collections is a
Class
Errors
Compilation issues, often caused by invalid syntax
Anonymous inner class
Constructs a new object of a new class, declared without a name
What will the following code print? class Base { public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } class Main { public static void main(String[] args) { Base b = new Derived(); b.show; } }
Derived::show() called
MVC
Design pattern which separates the UI from the application data
Single Responsibility
Design principle: a class should have only one responsibility
Interface Segregation
Design principle: clients should not be forced to depend on interfaces they do not use
Dependency Inversion
Design principle: high level modules should not depend on low level modules - both should depend on abstraction.
Liskov Substitution
Design principle: objects should be replaceable with instance of their subclasses
Open-Closed
Design principle: software should be open for extension & closed to modification.
What will the following code print? final class Base { public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } class Main { public static void main(String[] args) { Base b = new Derived(); b.show; } }
Error: final cannot be overridden
What will the following code print when compiled and run? class Grandparent { public void Print() { System.out.println("Grandparent's Print()"); } } class Parent extends Grandparent { public void Print() { System.out.println("Parent's Print()"); } } class Child extends Parent { public void Print() { super.super.Print(); system.out.println("Child's Print()"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.Print(); } }
Error: super.super is not supported
To create a GUI using JavaFX, "JavaFX" class must be extended?
False
True or False Functional interfaces can have more than one abstract method
False
True or False Java Abstract Window Type (AWT) follows the Model View Controller (MVC) architectural pattern.
False
True or False Open closed principle says that: "Objects in a program should be replaceable with instance of their subtypes without altering the correctness of that program"
False
True or False The comparable interface in Java facilitates comparisons between objects and can compare two objects of two different classes.
False
True or False The compiler converts the source code line-by-line during run time
False
True or False class Test { public static void main(String[] args) { short s = 10; // 1 char c = s; // 2 s = c; // 3 } } Both lines 2 and 3 are valid?
False
Lambda expression type that accepts an integer and returns an integer
Function
Which of the following collection is not of Iterable type?
HashMap
Which of the following implementation will you use if you were to insert elements at any position in the collection?
LinkedList
What will the following code print when compiled and run? public class Paper { public String title; public int id; public Paper(String title, int id) { this.title = title; this.id = id; } public static void main(String[] args) { Paper[] papers = { new Paper("T1", 1), new Paper("T2", 2), new Paper("T3", 3) }; System.out.println(papers); System.out.println(papers[1]); System.out.println(papers[1].id); } }
Paper;@hashcode Paper@hashcode 2
Inheritance
Paradigm enabling subclass/superclass relationship
What will the following code print when compiled and run? class Grandparent { public void Print() { System.out.println("Grandparent's Print()"); } } class Parent extends Grandparent { public void Print() { System.out.println("Parent's Print()"); } } class Child extends Parent { public void Print() { super.Print(); system.out.println("Child's Print()"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.Print(); } }
Parent print() Child print()
Lambda expression type that accepts an integer and returns a boolean
Predicate
What will the following code print when compiled and run? import java.util.Scanner; import java.io.PrintWriter; import java.io.StringWriter; public class StringOutputStresm { public static void main(String[] args) { String item; int quantity; int discount; StringWriter itemCharStream = new StringWriter(); PrintWriter itemOSS = new PrintWriter(itemCharStream); item = "Shoes"; quantity = 12; discount = 25; itemOSS.println(item + " x" + quantity); itemOSS.print(discount + "% off"); System.our.print(itemCharStream.toString()); } }
Shoes x12 25% off
Subclass
The "child" class which may inherit methods from another class
Superclass
The "parent" class which may define methods to be inherited
True or False Open closed principle says that: "Objects in a program should be open to extension but closed to modification"
True
True or False The comparable interface in Java facilitates comparisons between objects and can compare the current object with another.
True
True or False The comparator interface in Java facilitates comparisons between objects and can compare two objects of two different classes.
True
True or False The compiler converts the source code in one go before run time
True
True or False The interpreter converts the source code line-by-line during run time
True
True or False The keyword static allows main() to be called without having to instantiate a particular instance of the class
True
True or False class Test { public static void main(String[] args) { short s = 10; // 1 char c = (char) s; // 2 s = c; // 3 } } Is line 2 valid?
True
Set
Type of data structure which contains no dublicates
List
Type of data structure which enables ordered organization
Map
Type of data structure which requires key/value pairs
Which of the following is true for the arguments in lambda expressions?
Type of the argument can be omitted
Yes or no What we call as JFrame in Swing is similar to Stage in JavaFX?
Yes
Shorter lambda expression that adds two numbers and return their sum
(a, b) -> a + b;
Longer lambda expression that adds two numbers and return their sum
(int a, int b) -> a + b;
What does this code output? public class Cat { public String name; public void parseName() { System.out.print("1"); try { System.out.print("2"); int x = Integer.parseInt(name); System.out.print("3"); } catch (NullPointerException e) { System.out.print("4"); } System.out.print("5"); } public static void main(String[] args) { Cat leo = new Cat(); leo.name = "Leo"; leo.parseName(); System.out.print("6"); } }
12 Error
The communication/wire between the scene builder and the controller class is done using the annotation?
@FXML
What is the return type of the equals() method of the Object class? A. boolean B. Object C. String D. Char
A
Which line of code will compile without RuntimeException? public interface Temperature { // 1 boolean hasScales(); // 2 public default getTempFahrenheit () { // 3 return 113.0; // 4 } public default void Scale(); // 5 public int getTempCelsius() { // 6 return 45; // 7 } } (hint 5 is correct) A. 2 B. 3 C. 6 D. 7
A
Abstract class
A Java class which cannot be instantiated, should be implemented
Collection
A framework that can hold groups of objects
Lambda expression
instances of functional interfaces
Create a Button called goButton with the label "Go!": Button goButton = ...
new Button("Go!");
The class name for the application should be SimpleDrawingAppFX
public class SimpleDrawingAppFX extends Application{
In the model view controller architectural pattern, the model corresponds to
source/storage
What is the name of the method that is used to start the execution of a thread?
start()
Given a Stage object named appStage, write a statement that sets the stage's title to "Employees".
appStage.setTitle("Employees");
Given a Stage object named appStage, write a statement that makes the stage visible.
appStage.show();