Object Oriented Programming (SWE211) Quiz
40 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Suppose the xMethod() is invoked from a main method in a class as follows:

public static void main(String[] args) {
    xMethod();
}

xMethod() is ______ in the class.

  • a static method (correct)
  • an instance method
  • None of them
  • a static method or an instance method
  • If a variable is declared static in a particular class, what does that mean?

  • It can only be changed by the instances of the class in which it is declared
  • Each instance of the class has its own copy of the variable
  • There is only one copy of it that all instances of the class can access or share (correct)
  • It cannot be changed
  • Declare and construct an ArrayList with an initial capacity of 20 references to Object.

  • `ArrayList list[20] = new ArrayList();`
  • `ArrayList<Object> list = new ArrayList<Object>(20);` (correct)
  • `Object list(20) = new ArrayList();`
  • `ArrayList[Object] list = new ArrayList(20);`
  • Which of these cannot be declared static?

    <p><code>Object</code> (D)</p> Signup and view all the answers

    Which of the following statements are true?

    i. A subclass is a subset of a superclass. ii. A subclass is usually extended to contain more functions and more detailed information than its superclass. iii. "class A extends B" means A is a subclass of B. iv. "class A extends B" means B is a subclass of A.

    <p>i, ii and iii only (D)</p> Signup and view all the answers

    Given a class named Student, which of the following is a valid constructor declaration for the class?

    <p><code>Student (Student s) { }</code> (C)</p> Signup and view all the answers

    Which option can we use in the try block to stop the execution of the "finally" block?

    try {
        // option
    } finally {
        // code
    }
    

    <p><code>No correct answer</code> (A)</p> Signup and view all the answers

    Suppose the xMethod() is invoked from a main method in a class as follows: public static void main(String[] args) { xMethod(); } xMethod() is ____ in the class.

    <p>a static method (C)</p> Signup and view all the answers

    Which option can we use in the try block to stop the execution of the "finally" block?

    <p>No correct answer (A)</p> Signup and view all the answers

    What is printed as a result of executing the following code segment? ArrayList p = new ArrayList(); p.add(7); p.add(2); p.add(2); p.remove(2); p.add(1); p.add(2); System.out.println(p);

    <p>[7, 2, 1] (C)</p> Signup and view all the answers

    What is printed as a result of executing the following code segment? ArrayList aList = new ArrayList<Integer>(); alist.add(1); alist.add(2); alist.remove(1); alist.add(1, 3); alist.set(1, 4); alist.add(5); System.out.println(aList);

    <p>[1, 4, 3, 5] (D)</p> Signup and view all the answers

    Examine the following code: ArrayList<String> list = new ArrayList<String>(); list.add("Andy" ); list.add("Bart" ); list.add("Carl" ); list.add("Doug"); list.add("Elmo" ); Which of the following will change the list so that it looks like: Andy Bart Carl Doug

    <p>list.remove(list.size()-1); (B)</p> Signup and view all the answers

    What is the output of the following java code?

    import java.util.ArrayList; class TestList { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("A"); al.add("B"); al.add("A"); al.add(null); System.out.println(al); } }

    <p>[A,B,A,null] (A)</p> Signup and view all the answers

    Examine the following code: public class Main { public static void main(String[] args) { List<Integer> mylist = new ArrayList(); System.out.println(mylist.add(5)); } } What is the output of this java code?

    <p>5 (A)</p> Signup and view all the answers

    Analyze the following code. public class Test { public static void main(String args[]) { NClass nc = new NClass(); nc.t = nc.t++; } } class NClass{ int t; private NClass() { } }

    <p>The program has a compile error because the NClass class has a private constructor. (B)</p> Signup and view all the answers

    Analyze the following code. double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list);

    <p>The code is correct and displays [1.0, 2.0, 3.0]. (B)</p> Signup and view all the answers

    Analyze the following code.

    public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } } class A { int i; public void m(int i) { this.i = i; } } class B extends A { public void m(String s) { } }

    <p>The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B. (D)</p> Signup and view all the answers

    Every JavaFX main class _____.

    <p>implements javafx.application.Application (B)</p> Signup and view all the answers

    Which of the following statements is true?

    <p>A Node can be placed in a Pane. (A)</p> Signup and view all the answers

    To place a node in the left of a BorderPane p, use _____.

    <p>p.setLeft(node); (A)</p> Signup and view all the answers

    Which of the following methods is NOT defined in the Animation class?

    <p>resume() (A)</p> Signup and view all the answers

    To remove a node from the pane, use _____.

    <p>pane.getChildren().remove(node); (B)</p> Signup and view all the answers

    Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?

    <p>DataOutputStream outfile = new DataOutputStream(new FileOutputStream(&quot;out.dat&quot;)); (B)</p> Signup and view all the answers

    After the following program is finished, how many bytes are written to the file t.dat? import java.io.*; public class Test { public static void main(String[] args) throws IOException { DataOutputStream output = new DataOutputStream(new FileOutputStream("t.dat")); output.writeShort(1234); output.writeShort(5678); output.close(); } }

    <p>8 bytes. (B)</p> Signup and view all the answers

    Interfaces can be instantiated.

    <p>False (B)</p> Signup and view all the answers

    The modifiers public and static can be written in either order “public static” or “static public”.

    <p>True (A)</p> Signup and view all the answers

    Abstraction is supported by method overriding in java.

    <p>False (B)</p> Signup and view all the answers

    You can override a static method defined in a superclass.

    <p>False (B)</p> Signup and view all the answers

    You can write a constructor for an abstract class.

    <p>True (A)</p> Signup and view all the answers

    If AbsClass is an abstract class, the expression x instanceof AbsClass is always false.

    <p>False (B)</p> Signup and view all the answers

    Classes, methods, and instance variables can all be declared final.

    <p>True (A)</p> Signup and view all the answers

    What does the instanceof operator evaluate to, if the left-hand-side object is a child of the right-hand-side class or is the same type as the class provided?

    <p>true</p> Signup and view all the answers

    Does the instanceof operator work with interfaces?

    <p>yes</p> Signup and view all the answers

    What error does the program give if instanceof operation is applied with a type that is always false?

    <p>compile time error</p> Signup and view all the answers

    A null reference is an instance of any class.

    <p>False (B)</p> Signup and view all the answers

    What is the common use case of instanceof operation?

    <p>to check the actual type of an object at runtime and perform different actions based on its type</p> Signup and view all the answers

    How do you count the number of lines in a text file?

    <p>Read the file line by line using a <code>Scanner</code> and increment a counter for each line.</p> Signup and view all the answers

    How do you get the file name as user input?

    <p>Prompt the user to enter the file name using <code>System.out.print</code> and store the input using a <code>Scanner</code>.</p> Signup and view all the answers

    Write a java method that takes an array of different elements' type and returns an array of only the ints from the array.

    <pre><code class="language-java">import java.util.ArrayList; import java.util.Arrays; public class JavaTest { public static int[] integers(Object[] in) { ArrayList&lt;Integer&gt; ints=new ArrayList&lt;&gt;(); for(Object o: in) if(o instanceof Integer) ints.add((Integer)o); int[] out = new int[ints.size()]; for(int i=0; i&lt;ints.size(); i++) out[i] = ints.get(i); return out; } public static void main(String[] args) { Object[] a = {89,&quot;hello&quot;,12,0.23,&quot;java&quot;}; int[] ints = integers(a); System.out.println(Arrays.toString(ints)); } } </code></pre> Signup and view all the answers

    Write a Java program that simulates a stock account with deposit and withdrawal operations. Use two threads: one for performing deposits and another for withdrawals. Ensure thread safety without using notify() or wait() methods.

    <pre><code class="language-java">class Stock { private int balance = 0; public synchronized void deposit(int amount) { balance += amount; System.out.println(Thread.currentThread().getName() + &quot; deposited: &quot; + amount + &quot;, Balance: &quot; + balance); } public synchronized void withdraw(int amount) { if (balance &gt;= amount) { balance -= amount; System.out.println(Thread.currentThread().getName() + &quot; withdrew: &quot; + amount + &quot;, Balance: &quot; + balance); } else { System.out.println(Thread.currentThread().getName() + &quot; insufficient balance to withdraw: &quot; + amount); } } } class DepositThread extends Thread { private Stock stock; public DepositThread(Stock stock) { this.stock = stock; } @Override public void run() { for (int i = 1; i &lt;= 5; i++) { stock.deposit(i * 10); // Deposit multiples of 10 try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.out.println(&quot;Thread interrupted: &quot; + e.getMessage()); } } } } class WithdrawThread extends Thread { private Stock stock; public WithdrawThread(Stock stock) { this.stock = stock; } @Override public void run() { for (int i = 1; i &lt;= 5; i++) { stock.withdraw(i * 15); // Withdraw multiples of 15 try { Thread.sleep(150); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.out.println(&quot;Thread interrupted: &quot; + e.getMessage()); } } } } public class StockExample { public static void main(String[] args) { Stock stock = new Stock(); DepositThread depositThread = new DepositThread(stock); WithdrawThread withdrawThread = new WithdrawThread(stock); depositThread.setName(&quot;DepositThread&quot;); withdrawThread.setName(&quot;WithdrawThread&quot;); depositThread.start(); withdrawThread.start(); } } </code></pre> Signup and view all the answers

    Flashcards

    What is a static method?

    A method that can be called without creating an instance of the class. It belongs to the class itself.

    What is an instance method?

    A method that needs an object of the class to be called. It operates on the object's data.

    What does it mean for a variable to be declared static?

    A static variable is shared by all instances of a class. Changes to it affect every object.

    How do you declare and construct an ArrayList with an initial capacity?

    An ArrayList is a dynamic list that can grow or shrink as needed. You can specify an initial capacity to reduce resizing overhead.

    Signup and view all the flashcards

    What cannot be declared static?

    Constants (final variables), methods can be declared static. Objects cannot. Variables can be declared static.

    Signup and view all the flashcards

    What is the relationship between a subclass and a superclass?

    A subclass inherits properties and methods from its superclass. It can extend the functionality with its own methods and data.

    Signup and view all the flashcards

    What does 'class A extends B' mean in terms of inheritance?

    The statement 'class A extends B' means that A is a subclass of B, inheriting from B's members.

    Signup and view all the flashcards

    What is a constructor in a class?

    A constructor is a special method that initializes the object when it is created. It has the same name as the class.

    Signup and view all the flashcards

    What is the 'finally' block in exception handling?

    The 'finally' block in a try-catch structure always executes, regardless of whether an exception is thrown or not.

    Signup and view all the flashcards

    Why is 'return' statement in the 'try' block not a valid way to prevent 'finally' block execution?

    The 'return' statement in the 'try' block will stop the execution of the 'finally' block immediately.

    Signup and view all the flashcards

    How do you remove the last element from a list?

    The 'remove' method removes an element at a specific index from a list. Using 'size() - 1' removes the last element.

    Signup and view all the flashcards

    How does the add method work in a list?

    The 'add' method adds an element to the end of a list. If the list already contains an element of the same value, it will be added to the end, not overwrite the preceding element.

    Signup and view all the flashcards

    How do you remove an element from the end of a list?

    The 'remove' method removes an element at a specific index from a list. Removing an element from the end of the list will shift the last element forward.

    Signup and view all the flashcards

    What does the 'clear' method do to a list?

    The 'clear' method removes all elements from a list.

    Signup and view all the flashcards

    What is the 'asList' method used for?

    The 'asList' method converts an array to a list. The elements of the array must be objects, not primitive data types.

    Signup and view all the flashcards

    What is method overloading?

    Method overloading means defining multiple methods with the same name but different parameter lists within a class.

    Signup and view all the flashcards

    What is method overriding?

    Method overriding occurs when a subclass provides a new implementation for a method that was already defined in its superclass. The signature of the method (name, parameters, and return type) must be the same.

    Signup and view all the flashcards

    What is the 'start(Stage s)' method in JavaFX?

    The 'start(Stage s)' method is called when the JavaFX application is launched. It is responsible for creating and showing the primary stage.

    Signup and view all the flashcards

    What is the 'Node' class in JavaFX?

    The 'Node' class is a base class for all visual elements in JavaFX. Panes, controls, shapes, text, and images are all subclasses of Node.

    Signup and view all the flashcards

    What is the 'Pane' class in JavaFX?

    The 'Pane' class is a container for other nodes in JavaFX. It provides layout and organization for its child nodes. Examples include the 'HBox' and 'VBox' for horizontal and vertical arrangements.

    Signup and view all the flashcards

    What is the 'Scene' class in JavaFX?

    The 'Scene' class represents a visual window in JavaFX. It contains a root node (a Pane) and holds the overall content of the window.

    Signup and view all the flashcards

    What is the 'Control' class in JavaFX?

    The 'Control' class is a subclass of Node that represents interactive elements like buttons, checkboxes, and text fields. It is used to receive input from the user.

    Signup and view all the flashcards

    What is the 'Shape' class in JavaFX?

    The 'Shape' class is a subclass of Node that represents two-dimensional geometric shapes like rectangles, circles, and lines.

    Signup and view all the flashcards

    How do you place a node on the left side of a BorderPane?

    To place a node on the left side of a BorderPane, use the 'setLeft(node)' method.

    Signup and view all the flashcards

    What is the 'Animation' class in JavaFX?

    The 'Animation' class provides the foundation for animations in JavaFX. It defines methods to control the execution of animations.

    Signup and view all the flashcards

    How do you remove a node from a pane in JavaFX?

    The 'remove(node)' method is used to remove a node from a pane.

    Signup and view all the flashcards

    What is the 'DataOutputStream' class?

    The 'DataOutputStream' class provides methods for writing primitive data types to an output stream in a binary format. It is a subclass of 'OutputStream'.

    Signup and view all the flashcards

    What is the 'FileOutputStream' class?

    The 'FileOutputStream' class is used to create and write data to a file.

    Signup and view all the flashcards

    What is the 'File' class?

    The 'File' class represents a file or directory in the file system. It provides methods to interact with files and directories.

    Signup and view all the flashcards

    Can an interface be instantiated?

    It is not possible to instantiate an interface, meaning you cannot create an object directly from an interface. Interfaces are used as blueprints for classes and can only be implemented by classes.

    Signup and view all the flashcards

    Can an abstract class have a constructor?

    Abstract classes can have constructors. These constructors are used to initialize the state of the abstract class before its subclasses extend it.

    Signup and view all the flashcards

    What is the 'instanceof' operator?

    The 'instanceof' operator checks if an object is an instance of a particular class or interface. It returns true if the object is an instance of the class or a subclass of that class, and false otherwise.

    Signup and view all the flashcards

    Study Notes

    Object Oriented Programming (SWE211)

    • Course name: Object Oriented Programming (SWE211)
    • Syllabus includes topics like A, Bus, 2A, B, C, 3A, B (likely course modules or assignments)
    • Final revision material is included.

    MCQ Questions and Answers (Various Topics)

    • Question 1 (Page 3): xMethod() invoked from a main method is either static or an instance method. The correct answer is "c. a static method or an instance method".
    • Question 2 (Page 4): A static variable in a class has only one copy shared among all instances. The correct answer is "a) There is only one copy of it that all instances of the class can access or share".
    • Question 3 (Page 4): To declare an ArrayList with an initial capacity of 20, use ArrayList<Object> list = new ArrayList<Object>(20);. The correct answer is "a".
    • Question 4 (Page 5): Objects, variables, and constants can be static. Methods cannot be declared static. The correct answer is "a".
    • Question 5 (Page 5): A subclass is an extension of a superclass, often with more functions and detail, and can be expressed as "class A extends B". If class A extends B, A is the subclass and B is the superclass. The correct answer for the second MCQ is "i, ii, and iii only"
    • Question 6 (Page 6): A valid constructor for a class named 'Student' is Student(){} (or possibly other forms). The correct answer is "d"
    • Question 7 (Page 7): Output question related to ArrayList operations and manipulation
    • Question 8 (Page 7): Output question related to ArrayList operations, including add, remove, add(index, element), and set.
    • Question 9 (Page 8): Given ArrayList code and output (page 8), the answer will depend on the operations called on the 'list'
    • Question 10 (Page 8): Given ArrayList code snippet (page 8), answer the question based on ArrayList operations (clear, remove, etc.)
    • Question 11 (Page 9): Output of a Java code snippet related to a List object. Determine what is printed. The correct answer is "a".
    • Question 12 (Page 9): Output of a Java code snippet related to a List object that uses ArrayList. Determine what is printed.
    • Question 13 (Page 10): Analysis of a Java code snippet (page 10); the code has a compile-time error (because the access modifier to the constructor in the NClass class is private)
    • Question 14 (Page 11): Java code analysis has a compile-time error as the original array is not an array of Objects. Correct answer is "A".
    • Question 15 (Page 12): Analysis of Java method overriding. The correct answer is "b".
    • Question 16 (Page 13): JavaFX main class overrides, implementation, or none. The correct answer is "b".
    • Question 17 (Page 13): Statement about placement of Node, Pane, Scene, or Control objects in JavaFX.
    • Question 18 (Page 14): Methods that are NOT defined in the Animation class
    • Question 19 (Page 14): Methods used to remove node from a JavaFX Pane.
    • Question 20 (Page 15): JavaFX program output
    • Question 21 (Page 16): Correct statement to create a DataOutputStream for 'out.dat'.
    • Question 22 (Page 17): Number of bytes written to a file named t.dat'.
    • Question 23 (Page 18): True or False Questions related to interfaces, modifiers, abstractions, method overriding, constructors, instanceof, classes, methods,instance variables, abstraction, and more.
    • Question 24 (Page 19): instanceof operator output
    • Question 25 (Page 20): instanceof operator output
    • Question 26 (Page 21): Analysis of Java instanceof statement. Output:Compile time error.
    • Question 27 (Page 22): instanceof null reference behavior
    • Question 28 (Page 23): instanceof statement output
    • Question 29 (Page 24): instanceof behavior with different types.
    • Question 30 (Page 25): Program to count lines in a text file
    • Question 31 (Page 26): Program to count words in a text file
    • Question 32 (Page 27): Program to count words by user-provided file input
    • Question 33 (Page 28): Java method to extract integers from an array
    • Question 34 (Page 29): Checking if a character in an array is a digit, whitespace, or letter
    • Question 35 (Page 30): General terms for user events, event handlers, and adding ActionListeners
    • Question 36 (Page 31): Creating a HashMap with iterator.
    • Question 37 (Page 32): Accessing the reference to the current thread
    • Question 38 (Page 33): Creating and running multiple threads, checking if threads run
    • Question 39 (Page 34): Creating threads, checking if threads are run, waiting for the threads to finish
    • Question 40 (Pages 35-38): Multithreaded program to simulate stock account operations

    Additional Topics (General)

    • Thread safety techniques
    • File input/output concepts
    • DataOutputStream, FileOutputStream

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This quiz covers multiple choice questions related to the Object Oriented Programming (SWE211) course syllabus, including key concepts like static methods, variables, and ArrayLists. It's designed for final revision, helping students solidify their understanding of critical topics before exams.

    More Like This

    Use Quizgecko on...
    Browser
    Browser