Font size
WorksheetsThird Quarter - Comprog Review
Total questions: 140
Worksheet time: 1hrs 18mins
When does Exceptions in Java arises in code sequence?
Run Time
Compilation Time
Can Occur Any Time
None of the mentioned
Which of these keywords is not a part of exception handling?
try
finally
thrown
catch
Predict the output of following Java program
class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
Got the Exception 10
Got the Exception 0
Compilation error
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 ");
}
}
}
Got the Test Exception
Inside finally block
Got the Test Exception
Inside finally block
Compiler Error
Output of following Java program?
class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}
Compilation Error
Compiles and runs fine
Compiles fine but throws ArithmeticException exception
class Test
{
public static void main (String[] args)
{
try
{
int a = 0;
System.out.println ("a = " + a);
int b = 20 / a;
System.out.println ("b = " + b);
}
catch(ArithmeticException e)
{
System.out.println ("Divide by zero error");
}
finally
{
System.out.println ("inside the finally block");
}
}
}
Compile error
Divide by zero error
a = 0
Divide by zero error
inside the finally block
a = 0
inside the finally block
Which of these is a super class of all errors and exceptions in the Java language?
RunTimeExceptions
Throwable
Catchable
None of the above
The built-in base class in Java, which is used to handle all exceptions is
Raise
Exception
Error
Throwable
Which of these keywords must be used to monitor for exceptions?
try
finally
throw
catch
Which of these keywords must be used to handle the exception thrown by try block in some rational manner?
try
finally
throw
catch
Which of these keywords is used to manually throw an exception?
try
finally
throw
catch
What will be the output of the following Java program?
class exception_handling {
public static void main(String args[]) {
try {
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e) { System.out.print("World");
}
}
}
Hello
World
HelloWorld
Hello World
What will be the output of the following Java program?
class exception_handling {
public static void main(String args[]) {
try {
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e) { System.out.print("B");
}
}
A
B
Compilation Error
Runtime Error
What will be the output of the following Java program?
class exception_handling {
public static void main(String args[]) {
try {
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e) { System.out.print("B");
}
finally {
System.out.print("C");
}
}
}
A
B
AC
BC
What will be the output of the following Java program?
class exception_handling {
public static void main(String args[]) {
try {
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e) { System.out.print("0");
}
System.out.print(sum);
}
}
0
05
Compilation Error
Runtime Error
Which of the following classes can catch all exceptions which cannot be caught?
RuntimeException
Error
Exception
ParentException
Which of the following operators is used to generate instance of an exception which can be thrown using throw?
thrown
alloc
malloc
new
Which of the following keyword is used by calling function to handle exception thrown by called function?
throws
throw
try
catch
Which of the following handles the exception when a catch is not used?
finally
throw handler
default handler
java run time system
Which part of code gets executed whether exception is caught or not?
finally
try
catch
throw
Which of the following should be true of the object thrown by a thrown statement?
Should be assignable to String type
Should be assignable to Exception type
Should be assignable to Throwable type
Should be assignable to Error type
At runtime, error is recoverable.
True
False
Which of these class is related to all the exceptions that can be caught by using catch?
Error
Exception
RuntimeExecption
All of the mentioned
Which of these class is related to all the exceptions that cannot be caught?
Error
Exception
RuntimeExecption
All of the mentioned
What exception thrown by parseInt() method?
ArithmeticException
ClassNotFoundException
NullPointerException
NumberFormatException
What will be the output of the following Java code?
class exception_handling {
public static void main(String args[]) {
try {
System.out.print("A");
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e) { System.out.print("B");
}
}
}
A
B
Hello
Runtime Exception
In which of the following package Exception class exist?
java.io
java.net
java.lang
java.util
Which exception is thrown when divide by zero statement executes?
NumberFormatException
NullPointerException
ArithmeticException
None of the listed options
When does Exceptions in Java arises in code sequence?
Run Time
Can Occur Any Time
Compilation Time
None of the mentioned
What is the output of this program?
class Main
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Finally
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
What will be the output of the following Java code?
public class San {
public static void main(String args[]) {
try {
System.out.print("Hello world ");
}
finally {
System.out.println("Finally executing ");
}
}
}
The program will not compile because no exceptions are specified
The program will not compile because no catch clauses are specified
Hello world
Hello world Finally executing
Which of these clause will be executed even if no exceptions are found?
throws
finally
throw
catch
A single try block must be followed by which of these?
finally
catch
finally & catch
none of the mentioned
Which of these exceptions will occur if we try to access the index of an array beyond its length?
ArithmeticException
ArrayException
ArrayIndexException
ArrayIndexOutOfBoundsException
What will be the output of the following Java code?
class exception_handling {
public static void main(String args[]) {
try {
int a = args.length;
int b = 10 / a;
System.out.print(a);
}
catch (ArithmeticException e) {
System.out.println("1");
}
}
}
Note : Execution command line : $ java exception_handling
0
1
Compilation Error
Runtime Error
What will be the output of the following Java code?
class exception_handling {
public static void main(String args[]) {
try {
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e) { System.out.print("B");
}
}
}
A
B
Compilation Error
Runtime Error
What will be the output of the following Java code?
class exception_handling {
public static void main(String args[]) {
try {
int a = 1;
int b = 10 / a;
try {
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
}
finally {
System.out.print("A");
}
}
catch (Exception e) { System.out.println("B");
}
}
}
A
B
AB
BA
What will be the output of the following Java code?
class exception_handling {
public static void main(String args[]) {
try {
int a = args.length;
int b = 10 / a;
System.out.print(a);
try {
if (a == 1)
a = a / a - a;
if (a == 2)
{
int []c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e) {
System.out.println("TypeA");
}
catch (ArithmeticException e) { System.out.println("TypeB");
}
}
}
Note: Execution command line: $ java exception_handling one two
TypeA
TypeB
Compilation Error
Runtime Error
What is the use of try & catch?
It allows us to manually handle the exception
It allows to fix errors
It prevents automatic terminating of the program in cases when an exception occurs
All of the mentioned
Which of these keywords are used for the block to be examined for exceptions?
try
catch
throw
check
Which of these statements is incorrect?
try block need not to be followed by catch block
try block can be followed by finally block instead of catch block
try can be followed by both catch and finally block
try need not to be followed by anything
What will be the output of the following Java code?
class Output {
public static void main(String args[]) {
try {
int a = 0;
int b = 5;
int c = a / b;
System.out.print("Hello");
}
catch(Exception e) { System.out.print("World");
}
}
}
Hello
World
HelloWOrld
Compilation Error
What will be the output of the following Java code?
class Output {
public static void main(String args[]) {
try {
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
}
}
Hello
World
HelloWOrld
Compilation Error
What will be the output of the following Java code?
class Output {
public static void main(String args[]) {
try {
int a = 0;
int b = 5;
int c = a / b;
System.out.print("Hello");
}
finally {
System.out.print("World");
}
}
}
Hello
World
HelloWorld
Compilation Error
What will be the output of the following Java code?
class Output {
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
catch(Exception e) { System.out.print("World");
}
finally {
System.out.print("World");
}
}
}
Hello
World
HelloWorld
WorldWorld
Which of these methods return description of an exception?
getException()
getMessage()
obtainDescription()
obtainException()
What will be the output of the following Java code?
class Myexception extends Exception
{
int detail;
Myexception(int a) {
detail = a;
}
public String toString() {
return "detail";
}
}
class Output {
static void compute (int a) throws Myexception {
throw new Myexception(a);
}
public static void main(String args[]) {
try {
compute(3);
}
catch(Myexception e) { System.out.print("Exception");
}
}
}
3
Exception
Runtime Error
Compilation Error
What will be the output of the following Java code?
class Myexception extends Exception {
int detail;
Myexception(int a) {
detail = a;
}
public String toString() {
return "detail";
}
}
class Output {
static void compute (int a) throws Myexception
{
throw new Myexception(a);
}
public static void main(String args[]) {
try {
compute(3);
}
catch(DevideByZeroException e) { System.out.print("Exception");
}
}
}
3
Exception
Runtime Error
Compilation Error
What will be the output of the following Java code?
class exception_handling {
public static void main(String args[]) {
try {
throw new NullPointerException ("Hello"); System.out.print("A");
}
catch(ArithmeticException e) { System.out.print("B");
}
}
}
A
B
Compilation Error
Runtime Error
Which of these class is highest in hierarchy in java ?
java.lang.Exception
java.lang.Error
java.lang.Throwable
java.lang.Object
Exception and Error are direct subclasses of?
BaseException
Throwable
Object
RuntimeException
What is the list nums if it is initially [5, 3, 1] and the following code is executed?
nums.add(6);
nums.add(0, 4);
nums.remove(1);
num.set(0, 5);
[4, 3, 1, 6]
[5, 3, 1, 6]
[4, 3, 6]
[4, 5, 3, 6]
What index value is used to locate the last element in the list ArrayList?
list.size()-1
list.length()-1
list.size()
list.length
Which statement below is the correct way to retrieve the second element in the list ArrayList?
list.get(1)
list[2]
list(1)
list[2]
Given the ArrayList list with the values [3, 7, 6, 0], what code below is the proper way to change the 0 to be a 9?
list.set(0, 9)
list.set(9, 0)
list.set(3, 9)
list[3] = 9
Given the nums ArrayList with the values [5, 4, 3, 2], what statement below removes the value 4 from the list?
nums.remove(4)
nums.remove(1)
nums.get(4) = null
nums.remove[1]
Given the nums ArrayList with the values [1, 2, 4, 5, 6], what code below can be used to add a 3 between the 2 and 4 in the list?
nums.add(2, 3)
nums.add(2, 4)
nums.add(3, 2)
nums.add(3)
What will print when the following code executes?
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(7);
list.add(8);
list.add(9);
list.add(1);
System.out.println(list.remove(1));
8
1
9
7
What is printed as a result of executing the following code?
ArrayList<Integer> alist = new ArrayList<Integer>();
alist.add(1);
alist.add(2);
alist.remove(1);
alist.add(1, 3);
alist.set(1, 4);
alist.add(5);
alist.set(0, 2);
System.out.println(alist);
[1, 4, 5]
[1, 4, 3, 5]
[2, 4, 5]
[2, 4, 3, 5]
What is printed as a result of executing the following code segment?
ArrayList<Double> vals = new ArrayList<Double>();
vals.add(3.5);
vals.add(4.5);
vals.add(5.5);
vals.add( vals.set(0, 2.5) );
vals.remove(3);
System.out.println(vals);
[2.5, 4.5, 5.5, 3.5]
[2.5, 3.5, 4.5, 5.5]
[3.5, 4.5, 5.5, 2.5]
[2.5, 4.5, 5.5]
Which statement is the correct declaration and initialization of an ArrayList of Integer values?
ArrayList<Integer> name;
name = new ArrayList<Integer>();
ArrayList name;
name = new ArrayList<Integer>();
ArrayList<Integer> name;
name = ArrayList<Integer>();
Integer<ArrayList> name;
name = new Integer<ArrayList>();
What is printed as a result of executing the following code on an ArrayList of Integers called nums with the values [3, 2, -4, 8, 7]?
for (int idx = 0; idx < nums.size(); idx++)
if (nums.get(idx) % 2 == 0)
nums.add(99);
System.out.println(nums.size());
7
5
8
memory full because too many values added
list.get(index)
list[index]
list.getElement(index)
list.index
Which of these class is superclass of String and StringBuffer class?
java.util
java.lang
ArrayList
None of the mentioned
Which of these is an incorrect statement?
String objects are immutable, they cannot be changed
String object can point to some other reference of String variable
StringBuffer class is used to store string in a buffer for later use
None of the mentioned
What is the output of this program?
class String_demo{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
}
}
a
b
c
abc
What is the output of this program?
class String_demo {
public static void main(String args[]){
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
ABC
BCD
CDA
ABCD
What is the output of this program?
class String_demo{
public static void main(String args[]){
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
String s1 = "abcd";
int len1 = s1.length();
int len2 = s.length();
System.out.println(len1 + " " + len2);
}
}
3 0
0 3
3 4
4 3
Which of the following statements are incorrect?
String is a class
Strings in java are mutable
Every string is an object of class String
Java defines a peer class of String, called StringBuffer, which allows string to be altered
What is the output of following program?
class string_class{
public static void main(String args[]){
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
obj2 = " world";
System.out.println(obj + " " + obj2);
}
}
hello hello
world world
hello world
world hello
toString() method is defined in
java.lang.String
java.lang.Object
java.lang.util
None of these
The String method compareTo() returns
true
false
an int value
1
The class string belongs to ................. package.
java.awt
java.lang
java.applet
java.string
Is String a primitive data type?
Yes
No
Both
Which method to use to find length of a string?
length()
size()
long()
count()
Which data type gets returned from length() method in String class?
double
String
int
number
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
What is the correct way to find the length of "txt" string?
int len = length(txt);
float len = txt.length();
int len = txt.length();
double len = length(txt);
Which is correct method to convert String into uppercase
changeUpperCase()
convertUpperCase()
toUpper()
toUpperCase()
String txt = "Hello World";
System.out.println(txt.toUpperCase());
Choose the correct output.
"HELLO WORLD"
HELLO WORLD
Hello world
Hello World
Choose correct purpose of indexOf() in String class.
returns the index (the position) of the last occurrence of a specified text in a string
returns the character of the first occurrence of a specified character in a string
returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)
returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
Guess the output.
John.concat(Doe)
John Doe
JohnDoe
"John Doe"
String x = "10";
String y = "20";
String z = x + y;
Guess the output.
1020
10 20
30
"10 20"
What will be the output of below code?
String statement = "Outfit of the day";
System.out.println(statement.Substring(3,6));
it of
tfit
fit
FIT
What is the most appropriate way to check if two Strings are equal?
string1 == string2
string1.equals(string2)
string1.same(string2)
string1.equalsIgnorecase(string2)
Pick all NON-primitive datatypes from below :
String
int
primitive
Array
What is word.length()?
What is word.charAt(7) ?
What is word.startsWith("A few") ?
What is word.endsWith("man") ?
What is word.indexOf(“f”)?
What is word.indexOf(“bad”)?
What is word.substring(3, 7) ?
Nested classes are classes defined inside bottom-level classes.
True
False
Static nested classes can directly access the members of the class where are defined.
True
False
Top-level classes are easily specified inside a package.
True
False
Static nested classes are also defined within the top-level class but exist in isolation.
True
False
Non-static nested classes are declared inside the top-level classes.
True
False
To define a collection of constants, Java uses a special type known as _______.
enum
static
inner class
nested class
Enum keyword signifies a distinct data type that allows a variable to belong to a set of predefined _________.
enum
constants
static
collection
You cannot declare enum inside the class.
True
False
The values defined within an enum are constants and shall be entered in ___________ letters
lowercase
camel case
proper case
uppercase
If an enum is a member of a class, then it is indirectly classified as _______.
enum
constants
void
static
An inner class can be private
True
False
Once you declare an inner class private, it cannot be accessed from an object outside the class.
True
False
The user can also use enum in ____________ to obtain an array of possible values.
enumeration
iteration
nested class
inner class
Enum can used together with if and switch statements to compare a variable pointing to an enum constant.
True
False
Enum should be used when it is necessary to define and denote a fixed set of constants.
True
False
Which of the following is the SQL statement to verify that the table named "SUPPLY" is existing in the database?
SHOW SUPPLY;
VERIFY SUPPLY;
DESC SUPPLY;
DROP SUPPLY;
Which of the following is the correct SQL statement to delete the entire table named "WORKER"?
DROP TABLE WORKER;
DELETE DATABASE WORKER;
DELETE TB WORKER;
DELETE TABLE WORKER;
It is a column in a table that is designed to maintain specific information about every record in the table
Record
Field
Table
Row
It is also called a row of data is each individual entry that exists in a table
Record
Column
Database
Field
Which of the following is NOT an EXACT DATA TYPE?
smallint
real
tinyint
money
A character string data type with a maximum length of 2,147,483,647 characters.
varchar
char
text
smallmoney
Which of the following is the correct SQL statement to create a new database called STAFF?
CREATE DB STAFF;
CREATE DATABASE STAFF;
DB CREATE STAFF;
DATABASE CREATE STAFF;
Which of the following is the correct SQL statement to delete the database named "SCHOOL"?
DROP DB SCHOOL;
DROP DATABASE SCHOOL;
DELETE DB SCHOOL;
DELETE DATABASE SCHOOL;
Which of the following is the correct SQL statement to show all the existing databases?
USE DATABASES;
SHOW DATABASES;
SHOW DB;
DESC DATABASES;
Which of the following is NOT true about NULL?
A field with no value
A value in a field that appears to be blank
A field that contains spaces
It is different from zero value
What does SQL stand for?
Structured Query Language
Structured Question Language
Strong Question Language
Which SQL statement is used to extract data from a database?
SELECT
OPEN
GET
EXTRACT
Which SQL statement is used to update data in a database?
UPDATE
SAVE
MODIFY
SAVE AS
Which SQL statement is used to insert new data in a database?
INSERT INTO
ADD NEW
ADD RECORD
INSERT NEW
With SQL, how do you select a column named "FirstName" from a table named "Persons"?
SELECT FirstName FROM Persons
SELECT Persons.FirstName
EXTRACT FirstName FROM Persons
With SQL, how do you select all the columns from a table named "Persons"?
SELECT * FROM Persons
SELECT *.Persons
SELECT [all] FROM Persons
SELECT Persons
With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" is "Peter"?
SELECT * FROM Persons WHERE FirstName='Peter'
SELECT [all] FROM Persons WHERE FirstName='Peter'
SELECT * FROM Persons WHERE FirstName<>'Peter'
SELECT [all] FROM Persons WHERE FirstName LIKE 'Peter'
With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" starts with an "a"?
SELECT * FROM Persons WHERE FirstName LIKE 'a%'
SELECT * FROM Persons WHERE FirstName LIKE '%a'
SELECT * FROM Persons WHERE FirstName='%a%'
SELECT * FROM Persons WHERE FirstName='a'
With SQL, how do you select all the records from a table named "Persons" where the "FirstName" is "Peter" and the "LastName" is "Jackson"?
SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson'
SELECT FirstName='Peter', LastName='Jackson' FROM Persons
SELECT * FROM Persons WHERE FirstName<>'Peter' AND LastName<>'Jackson'
With SQL, how do you select all the records from a table named "Persons" where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen"?
SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'
SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons
Which SQL statement is used to return only different values?
SELECT DISTINCT
SELECT DIFFERENT
SELECT UNIQUE
Which SQL keyword is used to sort the result-set?
ORDER BY
SORT BY
ORDER
SORT
With SQL, how can you return all the records from a table named "Persons" sorted descending by "FirstName"?
SELECT * FROM Persons ORDER BY FirstName DESC
SELECT * FROM Persons ORDER FirstName DESC
SELECT * FROM Persons SORT BY 'FirstName' DESC
SELECT * FROM Persons SORT 'FirstName' DESC
With SQL, how can you insert a new record into the "Persons" table?
INSERT INTO Persons VALUES ('Jimmy', 'Jackson')
INSERT ('Jimmy', 'Jackson') INTO Persons
INSERT VALUES ('Jimmy', 'Jackson') INTO Persons
With SQL, how can you insert "Olsen" as the "LastName" in the "Persons" table?
INSERT INTO Persons (LastName) VALUES ('Olsen')
INSERT INTO Persons ('Olsen') INTO LastName
INSERT ('Olsen') INTO Persons (LastName)
How can you change "Hansen" into "Nilsen" in the "LastName" column in the Persons table?
UPDATE Persons SET LastName='Nilsen' WHERE LastName='Hansen'
UPDATE Persons SET LastName='Hansen' INTO LastName='Nilsen'
MODIFY Persons SET LastName='Nilsen' WHERE LastName='Hansen'
MODIFY Persons SET LastName='Hansen' INTO LastName='Nilsen
With SQL, how can you delete the records where the "FirstName" is "Peter" in the Persons Table?
DELETE FROM Persons WHERE FirstName = 'Peter'
DELETE ROW FirstName='Peter' FROM Persons
DELETE FirstName='Peter' FROM Persons
With SQL, how can you return the number of records in the "Persons" table?
SELECT COUNT(*) FROM Persons
SELECT LEN(*) FROM Persons
SELECT NO(*) FROM Persons
SELECT COLUMNS(*) FROM Persons
Which operator is used to select values within a range?
BETWEEN
WITHIN
RANGE
Which operator is used to search for a specified pattern in a column?
FROM
LIKE
GET
