0% found this document useful (0 votes)
50 views9 pages

Progress-Test1 K15

Uploaded by

dihoc77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views9 pages

Progress-Test1 K15

Uploaded by

dihoc77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1) Which option is true for OOP’s characteristics?

(Choose 2)
a. Encapsulation is hiding the implementation details of a class
b. Encapsulation is combining data field and behavior in one class
c. Abstraction is the process of exposing the essential details of an entity,
while ignoring the irrelevant details, to reduce the complexity for the users.
d. Abstraction is taking on different meanings by an entity in different
contexts.
2) What will happen when you compile and run the following code?
class Shape{
String name;
public Shape(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
public class Test{
public static void main(String[] args){
Shape s1 = new Shape("Square");
changeShape(s1);
System.out.print(s1.getName());
}
public static void changeShape(Shape s){
s.setName("Circle");
System.out.print(s.getName());
}
}
a. Compilation error
b. SquareCircle
c. CircleCircle
d. CircleSquare
3) What is the output of following code?
class Test {
private int a, b;
public Test(int a, int b) {
this.a = a;
this.b = b;
}
public void view(int b) {
int a = 3;
System.out.println(a*b);
}
public static void main(String args[]) {
Test t = new Test(1,5);
t.view(2);
}
}
a. 5
b. 2
c. 6
d. 10
4) Predict the output of following Java program?
class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t;
System.out.println(t.i);
}
a. 0
b. Runtime error
c. Compilation error
d. Garbage value
5) What kind of variables a class can consist of?
a. Class variables, Instance variables
b. Member variables, Local variables, Parameters
c. Class variables
d. Class variables, Local variables, Instance variables, Parameters
6) What are true for “static” keyword in Java?
a. We can have static block in a class.
b. We can define static block inside a method
c. We can have static method implementation of an interface
d. The static block in a class is executed every time an object of class is
created
7) What will happen when you compile and run the following code?
public class Test{
public static void compute(int i, int j){
System.out.println("int version");
}
public void compute(int i, int j){
System.out.println("long version");
}
public static void main(String args[]){
Test t = new Test();
compute(10, 5);
}
}
a. int version
b. Runtime error
c. Compilation error
d. long version
8) What will happen when you compile and run the following code, if there is any
error, how to fix it?
public class Test {
class TestInner{
void sayHi(){
System.out.println("Hi");
}
}
}
public class Demo {
public static void main(String[] args) {
TestInner inner = new TestInner();
inner.sayHi();
}
}
a. Hi
b. Compile-time error.
Test.TestInner inner = new Test().new TestInner();
c. Compile-time error.
Test.TestInner inner = new Test.TestInner();
d. Runtime-time
9) In Java, when we implement an interface method, it must be declared as:
a. private
b. protected
c. public
d. final
10) What will happen if we try to compile and run below program?
interface Foo{
int x = 10;
}
public class Test {
public static void main(String[] args) {
Foo.x = 20;
System.out.println(Foo.x);
}
}
a. Prints 10
b. Prints 20
c. Compile-time error
d. Runtime error
11) Which of the following concept is often expressed by the phrase ‘One interface,
multiple methods’?
a. Abstraction
b. Polymorphism
c. Inheritance
d. Encapsulation
12) What restriction is there on using the super(..) reference in a constructor?
a. It can only be used in the parent's constructor
b. Only one child class can use it
c. It must be used in the last statement of the constructor.
d. It must be used in the first statement of the constructor.
13) What will happen when you compile and run the following code?
class One{
public static void process(){
System.out.print("Parent");
}
}
class Two extends One{
public static void process(){
super.process(); //non-static variable super cannot be referenced form a static context
System.out.print("Child");
}
public static void main(String args[]){
One one = new Two();
one.process();
}
}
a. Parent
b. Compilation error
c. ParentChild
d. Child
14) What will happen when you compile and run the following code?
abstract class One{
public void sayHello(){
System.out.println("Hello");
}
public abstract void sayHi();
}
public class Test{
public static void main(String[] args){
One o = new One();
o.sayHello();
}
}
a. Compile-time error: One is abstract, cannot be instantiated
b. Hello
c. Compile-time error: Does not override abstract method sayHi()
d. Runtime error
15) Which of the following is true about methods in an interface in Java? (Choose 2)
a. An interface contains only abstract methods
b. We can define a default and static method in an interface
c. private and protected access modifiers can also be used to declare methods
in interface
d. An interface can contain class constants, implied as final and static
16) What is the output from the following code?
class Product{
public void output(){
outputPrice();
outputDetail();
}
void outputPrice(){
System.out.print("Nothing.");
}
static void outputDetail(){
System.out.println("Nothing.");
}
}
class Car extends Product {
public void outputPrice(){
System.out.print("Car’s price.");
}
//Hide subclass static method, not override superclass static method.
//The static method is resolved at compile time cannot be overridden by a subclass.
//An instance method is resolved at runtime can be overridden.
public static void outputDetail(){
System.out.println("Car’s detail.");
}
public static void main(String[] args) {
Product car = new Car();
car.output();
}
}
a. Car’s price.Nothing.
b. Nothing.Nothing.
c. Nothing.Car’s price.
d. Car’s price.Car’s detail.
17) Consider the following two statements
Integer x = 25;
Integer y = new Integer(33);
Which of the following statements is true:
a. Both statements use boxing
b. Statement 1 uses boxing
c. Both statements use unboxing
d. Statement 2 uses autoboxing
18) You have the following code:
String s = "Hello";
String t = " " + "my" + " ";
s.concat(t);
s.toLowerCase();
s += "friend";
System.out.println(s);
what will the output be?
a. Compile error
b. Hello my friend
c. hello my friend
d. Hellofriend
19) What is the output from the following code
class A {
public int x;
protected int y;
public A(int xx, int yy) {
x = xx;
y = yy;
}
}
class B extends A{
int z;
public B() {
super(1,2);
z = 3;
System.out.println(x +"y"+z);
}
public static void main(String[] args) {
Object obj = new B();
}
}
a. 123
b. 4y
c. 1y3
d. Compile-time error
20) Which option is true for abstract class?
a. A non-abstract subclass of an abstract class must define all abstract methods
of super class
b. An abstract class can be used to initiate a object
c. Abstract classes can not be subclassed
d. An abstract class cannot have methods declared with implementation

You might also like