CSCI201 Midterm
Assume your program is running from the directory /usr/java/programs/. If you want to read the file /usr/input.txt, all of the following paths would work except...
../../input.txt
What is the smallest number of threads running at any time in a java program?
2
Assume a host has an IP address of 74.125.127.104 with a subnet mask of 255.255.255.192 (also written as 74.125.127.104/26). What is the network address ?
74.0.0.0
By convention in Java, all classes start with...
A capital letter
Which of the following is true about java interfaces?
A class can implement as many interfaces as it wants, Not all methods in an instance must be implemented for an abstract class, and Interfaces can extend other interfaces by using the "extends" keyword
Which of the following is incorrect about Threads with priorities?
A lower priority thread cannot get executed before a higher priority thread that is in ready state
What is a Race Condition?
A state where multiple threads are accessing the same variable and overwriting the other thread's updates
Which of the following statements regarding semaphores is correct?
A thread can release permits on a semaphore even without having them; A thread must acquire one of the permits of the semaphore before executing code managed by a semaphore; The number of permits available on a semaphore is specified at creation
What is the relationship between Thread and Runnable?
A thread object is also runnable
Polymorphism means that...
A variable of a supertype can refer to a subtype object
What is object serialization?
A way to transform an object into a sequence of bytes
If there are three threads each with three lines of code as follows: Thread A ( A1 A2 A3 ) Thread B ( B1 B2 B3 ) Thread C ( C1 C2 C3 ) Which of the following cannot be a possible execution?
A1, A2, B2, C1, B1, A3, C2, C3, B3
What is the picture illustrating?
Acceptance Testing
How are Java objects allocated?
All Java objects are dynamically-allocated
A class is encapsulated if...
All data is declared private
When must a Java constructor share the name of the class?
Always
Which of the following is true about abstract classes?
An abstract class can be inherited
Which of the following data types cannot be used with generics?
Array
A parent class is also known as what?
Base class (or super class)
How does a thread access code managed by a semaphore?
By acquiring a semaphore permit
What's the output of the following code? public class Test Thread extends Thread { public static void main( String argv[ ]) { Test Thread t = new TestThread(); t.run(); t.start(); } public void run() { System.out.println("CSCI201 is amazing"); } }
CSC1201 is amazing CSCI201 is amazing
What is the motivation for using Generics?
Compile-time type safety
An interface may...
Contain constants and static methods
The compute() method...
Contains the main computation performed by the task
In Java, how do we create a thread?
Create a class with a function called run() and implement the Runnable interface
What problem can the consumer/producer cause?
Deadlock
Which Java IDE will be used in the class and by the graders?
Eclipse
What is the problem faced with synchronized Collections object?
Even though we can get a synchronized Collections object, the iterator is fai-fast (not synchronized)
True or False: Finding a route is a very complicated process, and although you can find it running traceroute on a Mac or Linux computer from the terminal, there are no commands that can be used to find a route on Windows.
False
True or False: If a method only exists in the child and you have an instance of a parent, you can still call the method from the child.
False
True or False: Statements placed in the finally block will only execute if no exception has been caught.
False
True or False: Threads have priorities that the system imposes over them according to the work requested by them. The user does not have the option to set threads' priorities.
False
What is the difference between Fork/Join and Thread Pool?
Fork/Join works on divide and conquer whereas ThreadPool can be used solve independent tasks separately
Which of the following is not a way to solve the producer/consumer problem?
ForkJoinTask
What type of IP is 128.125.253.146?
IPV4
In the consumer/producer problem, (in general) what causes a deadlock to happen?
If the consumer is waiting for the producer while the producer is waiting for the consumer
Select the incorrect statement from the following options.
If you fork more threads than your CPU's, the threads will execute concurrently
A synchronized instance method ____________ acquires a lock on the instance before it executes the method.
Implicitly
Which of the following is NOT an access modifier in Java?
Imported
Which of the following is true about Java inheritance?
Java supports single inheritance
Which of the following is the correct way of releasing locks?
Locks are released in the reverse order of how they were acquired
Java enables you to acquire ____________ explicitly, which give you more control for coordinating ____________.
Locks, Threads
What is a thread pool used for?
Managing the number of threads executing concurrently; If a thread in a pool completes execution, it can be reused instead of needing to create another thread; If a thread terminates due to failing, another thread will be created to replace it
interface Parent { public int meth1(); } abstract class Child implements Parent { public abstract int meth2(); public abstract int meth1(); public bool meth3() { return true; } } class GrandChild extends Child { public int meth2() { return 30; } }
Meth1() in Grandchild is not implemented
____________ of an object operate on the data that is part of the object or data that is passed into the method (and usually on both).
Methods
Which of the following is a characteristic of concurrent programming?
Multi-threading Programming, Parallel Programming, Distributed Programming
What are semaphore example applications?
Multiplayer games that only allow a certain number of simultaneous users; Producer/Consumer problem could use semaphores instead of blocking queues; Simulating a factory building computers with a finite number of components where each computer needs a certain number of each component
A monitor is an object with ____________ exclusion and synchronization capabilities.
Mutual
A package in Java is similar to what C++ concept?
Namespace
Where are files stored in a computer system?
Non-volatile memory
The lock for a synchronized, non-static method is on the ____________, and for a synchronized, static method it is on the ____________.
Object, class
Which of the following is the correct way of deserializing an object?
ObjectInputStream in = new ObjectiveInputStream(new FileInputStream("output_ser")); int [ ] ar = ( int [ ] in.readObject());
Which of the following is the correct way of serializing an object ?
ObjectOutputStream out = new ObjectOutputStream(newFileOutputStream("output.ser")); out.writeObject(new int [ ] { 1,2,3 });
Exceptions are ____________.
Objects of a class
Which of the following is NOT true about semaphores?
Only one permit is available on a semaphore
Which of the following is a difference between Parallel and Distributed Programming?
Parallel programming typically utilizes different CPU's on the same machine, while distributed typically dispatches work to other computers
Which of the following statements regarding networking is/are true?
Ports allow us to uniquely identify network programs running on a single computer
What is the signature of the method that acts as the entry point into a java program?
Public static void main(String [ ] args)
Which state is the thread in when it has been created and started?
Ready
What type of inheritance does Java support?
Single
Why do we have to implement the abstract methods in the (non abstract) child class?
So that the code can compile and because otherwise, there won't be any implementations for the abstract methods
What is the purpose of distributed programming?
Speed up execution of your program by using resources typically on other computers
Which thread method do you call if you want to begin a thread's execution?
Start
High priority threads that never sleep or yield take up CPU time and don't allow lower priority threads access is a common example of
Starvation
What does System.gc() do?
Strongly hints the JVM to collect garbage
When a method with the ____________ keyword in its signature is running, only one thread can be inside that method at a time.
Synchronized
What is not an advantage of multithreading?
Testing/debugging is usually relatively straightforward, in comparison to other approaches
What is the Scanner class in Java?
The Scanner class is used to read input of different primitive data types
When in a try clause, where do you go if an exception is thrown?
The catch clause
When a method is called on an instance of a derived class, which definition of the method is called?
The method from the runtime type class
What happens if a thread calls await() and doesn't receive a signal?
The thread remains in the waiting state forever
What is the purpose of a try-catch statement?
To handle exceptions without halting the program
True or False: A fully encapsulated class has all of the data declared private and getters and setters must be provided to manipulate each piece of data.
True
True or False: An object with the compile-time type of a parent can take on the form of a child at runtime.
True
True or False: If signal() or signalAll() is never called on the condition, and the thread is never interrupted, then the thread will wait forever.
True
True or False: Multiple threads can be inserting and taking elements concurrently from BlockingQueue, without any concurrency issues arising.
True
True or False: Scrum focuses on managing iterative development rather than specific technical approaches to agile software engineering.
True
Which of the rule/rules belong to Multithreading Programming?
Use one lock for one resource; Always acquire locks in the same order in different threads; Always release locks in the opposite order they were acquired; If acquiring multiple locks, never release the outer lock without releasing the inner lock first; Always synchronize iterating through a data structure if the variable is shared across multiple threads; Make a critical section as small as possible to allow for the most concurrency in your program; Use the Collections framework for creating thread-safe data structures instead of Vector, Stack, and Hashtable
If a thread cannot acquire the monitor on a synchronized block of code, to what state does the thread move?
Waiting
Which of the following is NOT a valid transition between threads?
Waiting -> sleeping
What is the correct way to implement the fork/join framework so that we can process asynchronously and return the value?
We override the compute() method and call the invoke() method on the ForkJoinPool object
How does a thread obtain the lock for an object?
When a thread enters a synchronized block of code, it obtains the lock for that object
____________ is an example of polymorphism in java.
When two subclasses override a method from their common superclass with different implementations
What is the motto of Java?
Write once, run anywhere
Does the constructor have the same name as the class we create it in, and does it return anything?
Yes, No
If thread is a Thread object, calling thread.sleep(10) will cause the thread to sleep for __________.
at least 10 milliseconds
Which of the following is NOT a method for monitors?
awake()
A monitor...
can be any object.
What should you do after writing to a data stream?
flush() the buffer to make sure all changes are saved
A synchronized instance method __________ acquires a lock on the instance before it executes the method.
implicitly
How can a Condition object for a Lock named lock be created?
lock.newCondition()
Conditions are objects created by invoking the ____________ method on a ____________ object.
newCondition(), Lock
What is the difference between the next( ) and nextLine( ) methods in the Scanner class?
next( ) reads up until the first space, whereas nextLine( ) reads an entire line
When using monitors , what does calling notify() do?
notify() wakes up a single thread waiting on this object's monitor
When using monitors, what does calling notify() do?
notify() wakes up a single thread waiting on this object's monitor
signal() will unlock ____________.
one thread waiting on the lock
A ____________ is a lock that does not need to be acquired more than once if entering a second critical section on the same lock.
reentrant lock
True or False: Semaphores are used to control the number of threads that can access a shared resource.
true
A blocking queue causes a thread to block (i.e. move to the waiting state)...
when you try to add an element to a full queue or to remove an element from an empty queue
A blocking queue causes a thread to block (i.e. move to the waiting state):
when you try to add an element to a full queue or to remove an element from an empty queue