CS 151 Final

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Person ————— - name ————— + getName() : String + toString() : String ^ | Student ————— - studentId : int ————— + getStudentId() : int + toString() : string Based on the above class diagram, fix the below code: Person s = new Student("Bob",3); s.getStudentId(); // make changes to this line

(Student) s.getStudentId();

BorderLayout arranges components in how many regions?

5

Which one method is used to set the visibility of the frame? A. setVisible(true) B. setVisible() C. show(true) D. setVisible(false)

A

What is the proper file name extension for a Java byte code compiled file? A. .jar B. .class C. .java D. .bytecode

B, .class

What are the disadvantages of Reflection in Java? A. Poor performance B. Security restrictions C. Security issues D. All of the above

D

True or false: An abstract method is just like a regular method. It has a method body.

False

True or false: Polymorphism prevents applications from being dynamic.

False

True or false: You cannot inherit behavior from an abstract class

False

What is the output from running this code? public class MyClass { private int a; public double b; public MyClass(int first, double second) { this.a = first; this.b = second; } public static void incrementBoth(MyClass c1) { c1.a = c1.a + 1; c1.b = c1.b + 1.0; } public static void incrementA(int first) { first = first + 1; } public static void incrementB(double second) { second = second + 1.0; } public static void main(String[] args) { MyClass c1 = new MyClass(10, 20.5); MyClass c2 = new MyClass(10, 31.5); incrementA(c2.a); incrementB(c2.b); System.out.println(c2.a + ", "+ c2.b); } }

10, 31.5

What is the result of running the code? (Hint: draw a memory model) public class MyClass { private int a; public double b; public MyClass(int first, double second) { this.a = first; this.b = second; } public static void main(String[] args) { MyClass c1 = new MyClass(10, 20.5); MyClass c2 = new MyClass(10, 31.5); c2 = c1; c1.a = 2; System.out.println(c2.a); } }

2

abstract class E {} class F extends E{} class G extends F { public G() {} public static void main(String[] args) { DataType obj = new G(); } } How many values can this DataType have? (How many reference types can we use to refer an object of type G). Do not consider Object class.

3

abstract class E {} class F extends E{} interface H {} class G extends F implements H { public G() {} public static void main(String[] args) { DataType obj = new G(); } } How many values can this DataType have? (How many reference types can we use to refer to an object of type G). Ignore Object class.

4

public class Myclass { static int a = 20; static int b = 30; static int c = 40; Myclass() { a = 200; } static void m1() { b = 300; } static { c = 400; } public static void main(String[] args) { Myclass m1 = new Myclass(); m1.m1(); Myclass m2 = new Myclass(); System.out.println(m2.a + m2.b + m2.c); } } What will be printed?

900

Select which option closely resembles Strategy pattern A. Encapsulates interchangeable behaviors and uses delegation to decide which one to use. B. Subclasses decide which concrete classes to create. C. Allows objects to be notified when state changes. D. is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements

A

The class diagram is a part of A. Structural UML diagram B. Behavioral UML diagram

A

Where are member variables declared? A. Inside a class definition but outside the definition of any method in the class. B. Inside any method within a class. C. Inside every method within a class.

A

Which among the following best defines single-level inheritance? A. A class inheriting a base class B. A class which gets inherited by 2 classes C. A class inheriting a derived class D. A class inheriting a nested class

A

Which of the following describes the Singleton pattern correctly? A. This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. B. This pattern creates object without exposing the creation logic to the client and refer to newly created object using a common interface. C. In this pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. D. This pattern is used when we want to pass data with multiple attributes in one shot from client to server.

A

Which of the following is not a facet of traditional object-oriented programming languages? A. Objects are grouped as procedures, separate from the data they act on. B. An object can hold data, referred to as attributes. C. An object can perform actions, via methods. D. An object can take many forms via casting.

A

default Layout Manager for Frames? A. BorderLayout B. FlowLayout C. GridLayout D. null

A

UML diagrams can help engineering teams. Select which are true: A. Plan out new features before any programming takes place. B. Communicate with technical and non-technical audiences more easily. C. Navigate source code. D. Bring new team members or developers switching teams up to speed quickly.

A, B, C, D

We can use reflection to get information about? A. Class B. Constructors C. Methods D. Instance variables (fields)

A, B, C, D

Which of the following is correct about generics? A. They add stability to your code by making more of your bugs detectable at compile time B. Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. C. With generics we can eliminate the use of casts D. Generics enable programmers to implement generic algorithms E. A generic type is a generic class or method that is parameterized over types.

A, B, C, D

class A { /* ... */ } interface B { /* ... */ } interface C { /* ... */ } Which of the following will not result in a compile-time error? A. class D <T extends A & B & C> { /* ... */ } B. class D <T extends A & C & B> { /* ... */ } C. class D <T extends B & A & C> { /* */} D. class D <T extends C & B> { /* */}

A, B, D

The idea behind object-oriented programming is to structure a program around the real-world concept of an object. Which of the following are the advantages of that approach? A. It can make the program easier for humans to think about. B. It usually makes the program run faster. C. It can help with debugging.

A, C

Which of these is a super class of all errors and exceptions in the Java language? A. Throwable B. RuntimeExceptions C. Catchable D. Object

A, D

What do these visibilities mean in UML? A. - B. + C. #

A. Private B. Public C. Protected

What do these UML multiplicities mean? A. 0..1 B. 1 C. 0..* D. * E. 1..n

A. Zero or one B. Only 1 C. Zero or more D. 1 or more E. 1 to n, where n>1

If a base class is inherited in protected access mode then which among the following is true? A. Private, Protected and Public all members of base, become private of derived class B. Public and Protected members of base class becomes protected members of derived class C. Only protected members become protected members of derived class D. Only private members of base, become private of derived class

B

Select which option closely resembles Factory pattern A. Encapsulates interchangeable behaviors and uses delegation to decide which one to use. B. Subclasses decide which concrete classes to create. C. Allows objects to be notified when state changes. D. is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements

B

When constructing objects, in what order are instance variables initialized? A. They are initialized starting with your subclass and then you work your way up to the Object class. (So instance variable in your Subclass are initialized first.) B. They are initialized starting at Object and working their way down through the inheritance hierarchy to your subclass. (So instance variable in your Subclass are initialized last.)

B

Which of the following statements is true? A. An object can be used to produce multiple classes. B. A class can be used to produce multiple objects

B

Which of these keywords is used to manually throw an exception? A. try B. throw C. finally D. catch E. throws

B

final class A { int i; } class B extends A { int j; System.out.println(j + " " + i); } class inheritance { public static void main(String args[]) { B obj = new B(); obj.display(); } } What is the result of this code? A. 0 0 B. Compilation error C. Something random D. Runtime error

B

A is an abstract class and B is an interface. C is a concrete class that extends A and implements B. Choose all that applies. A. Class A needs to have at least one abstract method. B. The interface name B cannot be used to declare an object reference variable of class C. C. A reference variable of class A can refer to an object of class C. D. Class C inherits all fields from A even though they are private. E. We can't instantiate an object of class A. F. Class C needs to implement all abstract instance methods defined in B.

B, C, E, F

Which of the following statements are correct? A. An abstract class cannot implement an interface. B. An abstract class can be extended by an abstract or a concrete class. C. An interface can be extended by another interface. D. An interface can be extended by an abstract class. E. An interface can be extended by a concrete class. F. A concrete class can be extended by an abstract or a concrete class.

B, C, F

If this is your constructor below, what will the compiler add to it? public Student() { this.setName("Default"); } A. public Student() { this(); this.setName("Default"); } B. public Student() { super(); this.setName("Default"); }

B, a call to super();

Person ————— - name ————— + getName() : String + toString() : String ^ | Student ————— - studentId : int ————— + getStudentId() : int + toString() : string Based on the class diagram above, how can you fix this code: Person s = new Student("Bob",3); s.getStudentId(); A. Inner classes B. Casting C. Encapsulation D. Abstraction

B, casting

Design patterns are divided into these three fundamental groups.

Behavioral, creational, structural

How to retrieve the class name in the reflection? A. getClass().getDeclaredFields() B. getClass().getFields() C. getClass().getName() D. new getClass()

C

If a derived class object is created, which constructor is called first? A. Derived class constructor B. Depends on how we call the object C. Base class constructor

C

Select which option closely resembles Observer pattern A. Encapsulates interchangeable behaviors and uses delegation to decide which one to use. B. Subclasses decide which concrete classes to create. C. Allows objects to be notified when state changes. D. is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements

C

What are the advantages of using reflection in java? A. Extensibility features B. Debugging and testing tools C. Both A and B D. None

C

When is it OK to have an overloaded method (i.e., method with the same name as another method) that has a different return type? A. Always B. Never C. Only when the parameter list is also different

C

Which concepts are the same? A. Encapsulation and Polymorphism B. Abstraction and Polymorphism C. Encapsulation and Abstraction D. Inheritance and Encapsulation

C

Which of the following is true about a class's constructor? A. A constructor is variable in a class that can be used to reference the objects B. A constructor is a special method used to invoke other methods in the same class. C. The constructor is a special method with the same name as the class and it's used to instantiate objects from that class

C

class Test extends Exception { } class Main { public static void main(String args[]) { try { throw new Test(); } catch(Test t) { System.out.println("Got the Test Exception"); } finally { System.out.println("Inside finally block "); } } } What is the result of this code? A. Got the Test Exception B. Runtime error C. Got the Test Exception\nInside finally block D. Compile time error

C

class overload { int x; double y; void add(int a , int b) { x = a + b; } void add(double c , double d) { y = c + d; } overload() { this.x = 0; this.y = 0; } } class Overload_methods { public static void main(String args[]) { overload obj = new overload(); int a = 2; double b = 3.2; obj.add(a, a); obj.add(b, b); System.out.println(obj.x + " " + obj.y); } } What is the result of this code? A. 6.4 6 B. 6.4 6.4 C. 4 6.4 D. 6 6

C

Package: sample, other1, sub1 Outside of package: other2, sub2 Sub1 —> sample Sub2 —> sample Sample ————— Public w; Protected x; Y; Private z; Variable Y can be accessed from which classes? A. Sub2 B. Other2 C. Sub1 D. Other1 E. Sample

C, D, E

What are the advantages of the MVC Design Pattern? A. Knowledge on multiple technologies becomes the norm. Developers using MVC need to be skilled in multiple technologies. B. The framework navigation can be complex because it introduces new layers of abstraction and requires users to adapt to the decomposition criteria of MVC. C. Models can have multiple views D. MVC enables logical grouping of related actions on a controller together. The views for a specific model are also grouped together. E. Multiple developers can work simultaneously on the model, controller and views.

C, D, E

Which of the following method signatures is a valid entry point in a Java application? A. public static void main() B. public void main(String[] args) C. public static final void main(String[] mydata) D. private static void start(String[] mydata)

C, main method must be public, static, and take in an array of String arguments.

A _______ pattern combines two or more patterns into a solution that solves a recurring or general problem.

Compound

public class A { int x = 20; public static void main(String[] args) { B b = new B(); System.out.println(b.x); A a = new A(); System.out.println(a.x); A a2 = new B(); System.out.println(a2.x); } } class B extends A { int x = 30; } What is the result of this code? A. Runtime error B. 20 30 20 C. 30 20 30 D. 30 20 20 E. Compile time error F. 30 30 30

D

Why do we use default methods in Interface?

Default methods are used when we have an interface which is already implemented by many other classes. If we need to make a change in the interface, we can use a default method to avoid having to edit every single implementing class. This prevents errors, saves time, and allows for backwards compatibility.

Many of the problems like synchronization, class loading issues, reflection, and serialization/deserialization issues—can all be solved by using an ______ to create your Singleton.

Enum

True or false: A class cannot implement more than one interface

False

class TrickyQuestion { public static void doWork() { doMoreWork(); } public void doMoreWork() {} public static void main(String[] args) { doWork(); } } Will this code compile? If not, why? Explain.

No, static methods can't call non static methods.

public class Person { private String name; public Person(String name) { this.name = name; } public boolean isAsleep(int hr) { return 22 < hr || 7 > hr; } public String toString() { return name; } public void status(int hr) { if (this.isAsleep(hr)) System.out.println("Now offline: " + this); else System.out.println("Now online: " + this); } public static void main(String[] args) { Person p; p = new Student("Sally"); p.status(1); } } class Student extends Person { public Student(String name) { super(name); } public boolean isAsleep(int hr) // override { return 2 < hr && 8 > hr; } } what will be printed?

Now online: Sally

For the below example which design pattern is most applicable: Newspaper or magazine subscriptions: A newspaper publisher goes into business and begins publishing newspapers. You subscribe to a particular publisher, and every time there's a new edition it gets delivered to you. As long as you remain a subscriber, you get new newspapers. You unsubscribe when you don't want papers anymore, and they stop being delivered. While the publisher remains in business, people, hotels, airlines, and other businesses constantly subscribe and unsubscribe to the newspaper.

Observer

What is abstraction? Give a real-life example of abstraction.

Sample answer: Abstraction is the process of taking a problem and reducing the amount of unneccessary details, leaving a generalized solution which can apply to similar problems. A real life example of abstraction can be a computer mouse, where we don't know everything about its inner workings but we do know the important details such as how to click, scroll, and move the mouse.

Distinguish between Abstract classes vs. Interfaces. Any 5 differences.

Sample answer: Interfaces can only have final variables, while abstract classes can have non final variables. Interfaces cannot have concrete methods, while abstract methods can have methods with bodies. Interfaces use the keyword "implements" while abstract classes use the keyword "extends". Abstract classes are declared with "abstract" while interfaces are declared with "interface". You can't create an instance of an abstract class, but you can have a reference type of the interface.

Which design pattern ensures that only one object of particular class gets created?

Singleton

What is type argument in Foo<String> ?

String

abstract class Person { abstract void method1(); public void method2() { System.out.print("Person 2 "); } public static void main(String[] args) { Person u = new Undergrad(); u.method1(); } } class Student extends Person { public void method1() { System.out.print("Student 1 "); method2(); } public void method2() { System.out.print("Student 2 "); } } class Undergrad extends Student { public void method2() { System.out.print("Undergrad 2 "); } } what will be printed?

Student 1 Undergrad 2

What is type parameter in the following code: Foo<T>

T

Explain the protected access specifier.

The protected access specifier allows access to a class or variable from the package it's located in and by the subclasses.

True or false: A class is a specification or blueprint or a design that contains details about how an object will be in our application.

True

True or false: An object is an instance of a class. Just like how a house or a building is an instance of some blueprint that was designed by some architect.

True

True or false: In software, objects come into existence ONLY when an application is running and they do all of the work in the application.

True

True or false: you cannot create an instance of an abstract class.

True

class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } } What is printed?

World


संबंधित स्टडी सेट्स

Chapter 14: Sales and Lease Contracts

View Set

Chapter 2 in IB textbook: Exercise Health

View Set

Ecosystem Stability:Ecological Succession

View Set