JAVA OOPS AND SQL
1) What is an object in Java?
A) A blueprint for creating classes
B) A template for creating methods
C) An instance of a class with its own state and behavior
D) A reserved keyword in Java
Answer: Option C
2) Which concept allows you to define multiple methods in OOP with the same name but different
parameters in the same class?
A) Polymorphism
B) Inheritance
C) Abstraction
D) Overloading
Ans-D
3) Which OOP concept allows you to define a new class based on an existing class, inheriting its
attributes and methods?
A) Abstraction
B) Polymorphism
C) Encapsulation
D) Inheritance
Answer: Option D
4) _____________ may also be defined as the process of identifying only the required characteristics of
an object, ignoring the irrelevant details.
A) Data Abstraction
B) Polymorphism
C) Inheritance
D) All of these
Ans-A
5) What is the output of the following program?
class T
{
private T(){
[Link]("hello world");
}
public static void main(String args[])
{
T t=new T();
}
}
a) prints nothing
b) hello world
c) compilation fails
d) none of these
Ans: b
6) What is the output of the following code?
class Test
{
private static void main(String[] args)
{
[Link]("Hello World!");
}
}
a) Hello World!
b) Compilation fails
c) Run time exception
d) Prints garbage value.
Ans: C
7) What is the output of the following program?
class Demo
{
String title;
int value;
public Demo()
{
title += " class";
}
public Demo(int value) {
[Link] = value;
title = "Demo";
}
}
class Test {
public static void main (String args[]){
Demo d = new Demo(5);
[Link]([Link]);
}
}
A) Demo class
B) Class Demo
C) Demo
D) Class
E) Compilation fails
Ans: C
8) Which of the following are true? (Choose all that apply)
class Test
{
public static void main(String[] args){
short numPets = 5; // line4
int numGrains = 5.6; //line5
String name = "Scruffy"; //line6
[Link](); //line7
[Link](); //line8
[Link](); //line9
}
}
A. Line 4 generates a compiler error.
B. Line 5 generates a compiler error.
C. Line 6 generates a compiler error.
D. Line 7 generates a compiler error.
E. Line 8 generates a compiler error.
F. Line 9 generates a compiler error.
G. The code compiles as is.
Ans: B,D,E
9) What is the output of the following program?
class Test
{
public static void main(String[] args){
int x=10;
int y;
[Link](x+y);
}
}
a) 0
b) 10
c) Garbage value
d) Compilation fails
e) Run time exception
Ans: D
10) Given:
class Plane {
static String s = "-";
public static void main(String[] args) {
new Plane().s1();
[Link](s);
}
void s1() {
try { s2(); }
catch (Exception e) { s += "c"; }
}
void s2() throws Exception {
s3(); s += "2";
s3(); s += "2b";
}
void s3() throws Exception {
throw new Exception();
}}
What is the result?
A. -
B. -c
C. -c2
D. -2c
E. -c22b
F. -2c2b
G. -2c2bc
H. Compilation fails
Answer:
B is correct.
Once s3() throws the exception to s2(), s2() throws it to s1(),
and no more of s2()’s code will be executed.
11)
Given:
try { int x = [Link]("two"); }
Which could be used to create an appropriate catch block? (Choose all that apply.)
A. ClassCastException
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionInInitializerError
F. ArrayIndexOutOfBoundsException
Answer:
C and D are correct.
[Link] can throw a NumberFormatException, and
IllegalArgumentException is its superclass (i.e., a broader exception).
12)
Given:
1. class Loopy {
2. public static void main(String[] args) {
3. int[] x = {7,6,5,4,3,2,1};
4. // insert code here
5. [Link](y + " ");
6. }
7. } }
Which, inserted independently at line 4, compiles? (Choose all that apply.)
A. for(int y : x) {
B. for(x : int y) {
C. int y = 0; for(y : x) {
D. for(int y=0, z=0; z<[Link]; z++) { y = x[z];
E. for(int y=0, int z=0; z<[Link]; z++) { y = x[z];
F. int y = 0; for(int z=0; z<[Link]; z++) { y = x[z];
Answer:
A, D, and F are correct.
A is an example of the enhanced for loop.
D and F are examples of the basic for loop.
13)
Which is/are correct statements about primary key of a table?
A)Primary keys can contain NULL values
B) Primary keys cannot contain NULL values.
C) A table can have only one primary key with single or multiple fields
D) A table can have multiple primary keys with single or multiple fields
Answer: B and C
14) A relational schema for a train reservation database is given below.
Passenger (pid, pname, age)
Reservation (pid, class, tid)
Table: Passenger
pid pname age
-----------------
0 Sachin 65
1 Rahul 66
2 Sourav 67
3 Anil 69
Table : Reservation
pid class tid
---------------
0 AC 8200
1 AC 8201
2 SC 8201
5 AC 8203
1 SC 8204
3 AC 8202
What pids are returned by the following SQL query for the above instance of the tables?
SLECT pid
FROM Reservation ,
WHERE class ‘AC’ AND
EXISTS (SELECT *
FROM Passenger
WHERE age > 65 AND
Passenger. pid = [Link])
(A) 1, 0
(B) 1, 2
(C) 1, 3
(D) 1, 5
Answer: (C)
Explanation:
When a subquery uses values from outer query, the subquery is called correlated subquery.
The correlated subquery is evaluated once for each row processed by the outer query.
15)
The Virtual Table that is created by data from the result of an
SQL Select Statement
A) Synonym
B) Data Base
C) Virtual table
D) Sequence
E) View
Ans-E
16) In Java, objects created on the heap from one thread can be accessed by another thread
A)always
B)never
C)sometimes
D)only from the main thread
Ans-D
17)
class Dog{
String name;
Dog(String str)
{
name = str;
}
void setName(String name)
{
[Link] = name;
}
String getName()
{
return name;
}
}
public class MyClass {
public static void main(String [] args)
{
Dog myDog = new Dog("Rover");
foo(myDog);
[Link]([Link]());
}
public static void foo(Dog someDog)
{
[Link]("Max");
someDog = new Dog("Fifi");
[Link]("Rowlf");
}
What will be the output of the above code
A) Rover
B) Max
C) Fifi
D) Rowlf
Ans- B
18)
class MyClass {
public static void main(String [] args)
{
try
{
int a=0;
int b = 5;
int c = b/a;
[Link]("Hello");
}
catch(Exception e) {
[Link]("World");
}
finally {
[Link]("World");
}
}
}
A)Hello
B)World
C)HelloWorld
D)WorldWorld
Ans- D
19)
class myClass {
public static void main(String [] args)
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
[Link](c1+ " "+c2);
}
}
A)E U
B)E E
C)U E
D)Compilation Error
Ans- A
20)
class MyClass
{
public static void main(String [] args)
{
try {
int a=0;
int b=5;
int c=a/b;
[Link]("Hello");
}
catch(Exception e)
{
[Link]("World");
}
}
}
A) Hello
B) World
C) HelloWorld
D) Compilation error
Ans- A
21)
class A{
int i;
int j;
A() {
i=1;
j=2;
}
}
class MyClass {
public static void main(String [] args) {
A obj1 = new A();
A obj2= new A();
[Link]([Link](obj2));
}
}
A)false
B)true
C)compilation error
D)run time error
Ans-A
22)
class MyClass {
public static void main(String [] args)
{
int x=9;
if(x==9) {
int x=8;
[Link](x);
}
}
}
A)9
B)8
C)Compilation error
D)none of these
Ans- C
23)
The condition controlling the loop is initially false.
Which loop will allow its body to execute ?
A)do-while
B)for
C)while
D)none mentioned
Ans- A
24)
final class A
{
int i;
}
class B extends A {
int j;
void display() {
[Link](j + " " +i);
}
}
class MyClass {
public static void main(String [] args)
{
B Obj = new B();
[Link]();
}
}
A)2 2
B)3 3
C)Compilation error
D)run time error
Ans- C
25) In Java multiple inheritance can be achieved by
A)extending 2 or more classes
B)extending one class and implementing 1 or more interfaces
C)cannot be achieved
D)using the multiple keyword when extending classes
Ans- C
26)1) In Java which of the following statements are true
A)Each thread gets created by extending thread class only
B)Threads contain many process
C)Each process has many threads
D)All allocation happens on the heap, the stack is never used
Ans- C
27) Which of these statements are correct ?
1) Static variables, are initialized when class is loaded
2) Static variables in a class are initialized before any object of that class can be created
3) Static variables in a class are initialized before any static method of the class runs
4) Static variables are initialized every time an instance of a class is created
5) static variables should always be marked final
A) Statements 1,2 and 3 are correct
B) Statements 2,3 and 4 are correct
C) Statements 3,4 and 5 are correct
D) All Statements are correct
Ans- A
28)
State the output of this code below when compiled and runned without any arguments like below
java myClass
class myClass {
public void main(String [] args)
{
[Link]("Hello "+ args[0]);
}
}
a) Hello.c
b) [Link]
c) Hello world
D) Runtime error
Ans- D
29) Which of the following statements are incorrect ?
Select one of the answers below :
A)public members of class can be accessed by any code in the program
B)private members of class can only be accessed by other members of the class
C)private members of class can be inherited by a sub class and become protected members in sub class
D)protected members of a class can be inherited by a sub class and become private members of the sub
class
Ans- C
30)
class MyClass {
public static void main(String [] args)
{
int a=5;
int b=10;
first: {
second: {
third: {
if(a==b>>1)
break second;
}
[Link](a);
}
[Link](b);
}
}
}
A)5 10
B)10 5
C)5
D)10
Ans-D
31)
class A
{
int i;
public void display() {
[Link](i);
}
}
class B extends A {
int j;
public void display() {
[Link](j);
}
}
class MyClass {
public static void main(String [] args ) {
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r=obj2;
[Link]();
}
}
A)1
B)2
C)Runtime error
D)Compilation error
Ans- B
32) which of the following java constructs help in achieving thread safety ?
A)Process
B)try/catch
C)Thread
D)Runnable
E)synchronized
F)none of these
Ans- E
33)
State the output of this program
class Base {
public void method () {
[Link]("Base");
}
}
public class MyClass extends Base {
protected void method() {
[Link]("My Class");
}
public static void main(String [] args)
{
MyClass obj = new MyClass();
[Link]();
}
}
A)Base
B)MyClass
C)BaseMyClass
D)Compilation error
Ans-D
34) when is the finalize() method called ?
A)when on object is created
B)when an object is about to get garbage collected
C)as soon as all object references are released
D)when any variable holding a reference to the object goes out of scope
Ans- B
35) How does java implement polymorphism ?
A)method overloading
B)method overriding
C)method hiding
D)none of these
36) The statement that is executed automatically by the system as a side effect of the modification of
the database is
(A) backup
(B) assertion
(C) recovery
(D) trigger
Answer: (D)
37) A __________ is a virtual table based on the result-set of an SQL statement.
A) Schema
B) View
C) Both Schema and View
D) Triggers
Ans-B
38) which of the following key can have a null value
A) Primary key
B) Unique
C) Both Primary Key and Unique
D) None of these
ANs-B
39) A relational database contains two tables employee and department in which
employee table has columns emp_no, name and dept_id and
department table has columns dept_id and dept_name.
The following insert statements were executed successfully to populate the empty tables:
Insert into department values (1, 'Engineering')
Insert into department values (2, 'Finance')
Insert into employee values (l, 'Natraj', 1)
Insert into employee values (2, 'Murali', 2)
Insert into employee values (3, 'Govind', 1)
How many rows and columns will be retrieved by the following SQL statement?
Select * from employee, department
A) 0 row and 0 columns
B) 3 rows and 4 columns
C) 5 rows and 6 columns
D) 6 rows and 5 columns
Ans-D
Explanation: Simple,Cartesian product of two tables will result
Rows = 3*2 = 6
Columns = 3+2 = 5
40)
Which of these is the primary key in the following relation :
Book(ISBN,Author,Title,Price,Published Year)
A)ISBN
B)Author
C)Title
D)None of these
Ans- A