1Which of the following is correct for a listener in context to event handling?
A listener is an object that is notified when an event occurs
2Which of the following exceptions is thrown in case when the file specified for within is not found?
FileNotFoundException
3In threading, which of the following is correct?
start() method creates new thread and calls code written in run() method
4Which of the following events is sent when a button is clicked?
ActionEvent
5American Express cards begin with a three followed by a four or seven and have 15 digits. Right a regular expression to verify an American Express credit card number.
^3[47][0-9]{13}\$
6Object of the matcher class is used to compile regular expressions.
False
7What statement will terminate another thread instance
Return
8The internet is a network of networks interconnected by switches
True
9It is generally not difficult to design a transport layer protocol to provide both high reliability and low delay for applications.
False
11Create a Regex to match "I want a bicycle.";
String string1 = "I want a bicycle.";
System.out.println(string1.matches("I want a bicycle\\.")); // true
12Write a regular expression that will match "I want a bike." and "I want a ball."
String string2 = "I want a bike.";
String string3 = "I want a ball.";
System.out.println(string2.matches("I want a (bike|ball)\\.")); // true
System.out.println(string3.matches("I want a (bike|ball)\\.")); // true
13Replace blanks w/ underscores in this string: Replace blanks w/ underscores
String string4 = "Replace all blanks with underscores.";
String replaced = string4.replaceAll(" ", "_");
System.out.println(replaced); // "Replace_all_blanks_with_underscores."
14Write a regular expression that matches the following string using string.matches() String string5 = "aaabccccccdddefffg";
System.out.println(string5.matches("aaabccccccdddefffg")); // true
15Write a regular expression that will only match the string 5 above in its entirety.
System.out.println(string5.matches("^aaabccccccdddefffg$")); // true
16Write a sample program (code snippet) for thread join
class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}
public class ThreadJoinExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
try {
t1.join(); // Wait for t1 to finish
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread continuing...");
}
}
17Create three threads: main, thread A and thread B. In the simulation you put thread aid to sleep for 10 seconds and after 10 seconds thread, a will wake up. After that thread B will join thread a. Make sure to handle exceptions such as one thread, interrupting another thread.
class A extends Thread {
public void run() {
try {
System.out.println("Thread A sleeping for 10 seconds...");
Thread.sleep(10000);
System.out.println("Thread A woke up!");
} catch (InterruptedException e) {
System.out.println("Thread A interrupted.");
}
}
}
class B extends Thread {
private Thread threadA;
public B(Thread threadA) {
this.threadA = threadA;
}
public void run() {
try {
threadA.join(); // Wait for Thread A to finish
System.out.println("Thread B started after Thread A.");
} catch (InterruptedException e) {
System.out.println("Thread B interrupted.");
}
}
}
public class ThreadExample {
public static void main(String[] args) {
A threadA = new A();
B threadB = new B(threadA);
threadA.start();
threadB.start();
}
}