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></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</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></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></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</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</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]</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]</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);</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]</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</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.</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].</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.</p> Signup and view all the answers

    Every JavaFX main class _____.

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

    Which of the following statements is true?

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

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

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

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

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

    To remove a node from the pane, use _____.

    <p>pane.getChildren().remove(node);</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;));</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.</p> Signup and view all the answers

    Interfaces can be instantiated.

    <p>False</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</p> Signup and view all the answers

    Abstraction is supported by method overriding in java.

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

    You can override a static method defined in a superclass.

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

    You can write a constructor for an abstract class.

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

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

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

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

    <p>True</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</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

    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