CS Fundamentals

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is a package? a. a collection of editing tools b. a collection of classes c. a collection of classes and interfaces d. a collection of interfaces

c

What will be the output of the following Java program? final class A { int i; } class B extends A { int j; System.out.println(j + " " + i); } class inheritance { public static void main(String args[]) { B obj = new B(); obj.display(); } } a. 2 2 b. 3 3 c. Runtime error d. Compilation error

d

Which of the following modifiers cannot be used for constructor? a. public b. protected c. private d. static

d

Which of the following restrictions are true on static methods? 1. They must access only static data. 2. They can only call other static methods. 3. They cannot refer to this or super. a. 1 and 2 b. 2 and 3 c. only 2 d. 1, 2 and 3

d

What is the output of the following program? public class Outer { private static int data = 10; private static int LocalClass() { class Inner { public int data = 20; private int getData() { return data; } }; Inner inner = new Inner(); return inner.getData(); } public static void main(String[] args) { System.out.println(data * LocalClass()); } } a. compilation error b. runtime error c. 200 d. None of these

c

Which of these is the correct way of calling a constructor having no parameters, of superclass A by subclass B? a. super(void); b. superclass.(); c. super.A(); d. super();

d

What is the output of the following program? public class Test{ private String function() { return ("CS"); } public final static String function(int data) { return ("CS150"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function()); } } a. compilation error b. runtime error c. CS d. None of these

c

Which of these jump statements can skip processing the remainder of the code in its body for a particular iteration? a. break b. return c. exit d. continue

d

What is the output of the following program? public class Test{ public int getData(String temp) throws IOException { return 0; } public int getData(String temp) throws Exception { return 1; } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.getData("CS150")); } } a. 0 b. 1 c. Compilation error d. runtime error

c

Which of these keywords are used for the block to be examined for exceptions? a. finally b. throw c. catch d. try

d

Which of these keywords can be used to prevent Method overriding? a. static b. constant c. protected d. final

d

Which of these keywords must be used to handle the exception thrown by try block in some rational manner? a. try b. finally c. throw d. catch

d

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"); } } } a. 3 b. Exception c. detail d. runtime error e. compilation error

d

What will be the output of the following Java program? class output { public static void main(String args[]) { double a, b,c; a = 3.0/0; b = 0/4.0; c = 0/0.0; System.out.println(a); System.out.println(b); System.out.println(c); } } a. NaN b. Infinity c. 0.0 d. all of these

d

What is Truncation in Java? a. Floating-point value assigned to a floating type b. Floating-point value assigned to an integer type c. Integer value assigned to a floating type d. Integer value assigned to an integer type

b

Which of the following is true about interfaces in java. 1) An interface can contain following type of members.....public, static, final fields (i.e., constants) ....default and static methods with bodies 2) An instance of interface can be created. 3) A class can implement multiple interfaces. 4) Many classes can implement the same interface.

1, 3, 4

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"); } } } a. 3 b. Exception c. detail d. runtime error e. compilation error

b

Output of following Java Program? class Base { public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } public class Main { public static void main(String[] args) { Base b = new Derived();; b.show(); } }

Derived::show() called

What will be the output of the following Java program? class comma_operator { public static void main(String args[]) { int sum = 0; for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1) sum += i; System.out.println(sum); } } a. 5 b. 6 c. 10 d. 15 e. compilation error

b

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"); } } } a. Hello b. World c. HelloWorld d. Hello World

b

What will be the output of the following Java program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } } a. 1 b. 2 c. 6 d. 7

b

Which of these selection statements test only for equality? a. if b. switch c. if and switch d. none of these

b

Which of these statements is incorrect? a. switch statement is more efficient than a set of nested ifs b. two case constants in the same switch can have identical values c. switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression d. it is possible to create a nested switch statements

b

Predict the output of the following program. abstract class demo{ public int a; demo() { a = 10; } abstract public void set(); abstract final public void get(); } class Test extends demo{ public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.set(20); obj.get(); } } a. a = 10 b. a = 20 c. compilation error d. runtime error

c

Exception created by try block is caught in which block? a. catch b. throw c. final d. final

a

If a class inheriting an abstract class does not define all of its functions then it will be known as? a. abstract b. a simple class c. static class d. none of these

a

What is the output of the following Java program? class variable_scope { public static void main(String args[]) { int x; x = 5; { int y = 6; System.out.print(x + " " + y); } System.out.println(x + " " + y); } } a. compilation error b. runtime error c. 5 6 5 6 d. 5 6 5

a

What is the output of the following program? class Derived { public void getDetails() { System.out.printf("Derived class "); } } public class Test extends Derived{ public void getDetails() { System.out.printf("Test class "); super.getDetails(); } public static void main(String[] args) { Derived obj = new Test(); obj.getDetails(); } } a. Test class Derived class b. Derived class Test class c. compilation error d. runtime error

a

What is the output of the following program? public class Test{ private String function(String temp, int data, int sum) { return ("QUIZ"); } private String function(String temp, int data) { return ("CS150"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function("HI", 0, 20)); } } a. QUIZ b. CS150 c. HI d. compilation error e. runtime error

a

What is the output? class Base { public static void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public static void show() { System.out.println("Derived::show() called"); } } class Main { public static void main(String[] args) { Base b = new Derived(); b.show(); } } a. Base::show() called b. Derived::show() called c. compilation error d. runtime error

a

What is the output? class Base { public void Print() { System.out.println("Base"); } } class Derived extends Base { public void Print() { System.out.println("Derived"); } } class Main{ public static void DoPrint( Base o ) { o.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); DoPrint(x); DoPrint(y); DoPrint(z); } } a. Base Derived Derived b. Base Base Derived c. Base Derived Base d. compilation error

a

What will be the output of the following Java code? class String_demo { public static void main(String args[]) { char chars[] = {'a', 'b', 'c'}; String s = new String(chars); System.out.println(s); } } a. abc b. a c. b d. c

a

What will be the output of the following Java code? class A { public int i; protected int j; } class B extends A { int j; void display() { super.j = 3; System.out.println(i + " " + j); } } class Output { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } a. 1 2 b. 2 1 c. 1 3 d. 3 1

a

What will be the output of the following Java program? class overload { int x; double y; void add(int a , int b) { x = a + b; } void add(double c , double d) { y = c + d; } overload() { this.x = 0; this.y = 0; } } class Overload_methods { public static void main(String args[]) { overload obj = new overload(); int a = 2; double b = 3.2; obj.add(a, a); obj.add(b, b); System.out.println(obj.x + " " + obj.y); } } a. 4 6.4 b. 6.4 6 c. 6.4 6.4 d. 6 6

a

What will be the output? class Base { final public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } class Main { public static void main(String[] args) { Base b = new Derived();; b.show(); } } a. Compilation error b. Base::show() called c. Derived::show() called d. runtime error

a

When an array is passed to a method, what does the method receive? a. The reference of the array b. A copy of the array c. Length of the array d. Copy of first element

a

When is the object created with new keyword? a. at run time b. at compile time c. depends on the code d. none of these

a

Arrays in Java are a. Object references b. objects c. primitive data type d. none of the above

b

How is time complexity measured? a. By counting the number of algorithms in an algorithm. b. By counting the number of primitive operations performed by the algorithm on a given input size. c. By counting the size of data input to the algorithm. d. None of these

b

What does it mean when we say that an algorithm X is asymptotically more efficient than Y? a. X will always be a better choice for small inputs b. X will always be a better choice for large inputs c. Y will always be a better choice for small inputs d. X will always be a better choice for all inputs

b

The compareTo() method returns a. Nothing b. true or false c. an int value d. none of these

c

What is the output? class Grandparent { public void Print() { System.out.println("Grandparent's Print()"); } } class Parent extends Grandparent { public void Print() { System.out.println("Parent's Print()"); } } class Child extends Parent { public void Print() { super.super.Print(); System.out.println("Child's Print()"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.Print(); } } a. Grandparent's Print()Parent's Print()Child's Print() b. Grandparent's Print()Child's Print() c. compilation error in super.super.Print(); d. runtime error

c

What is the time complexity of the following code: int i, j, k = 0; for (i = n / 2; i <= n; i++) { for (j = 2; j <= n; j = j * 2) { k = k + n / 2; } } a. O(n) b. O(n log n) c. O(n^2) d. O(n^2Logn)

c

What is the time, and space complexity of the following code: int a = 0, b = 0; for (i = 0; i < N; i++) { a = a + Math.random(); }for (j = 0; j < M; j++) { b = b + Math.random(); } a. O(N * M) time, O(1) space O(N * M) time, O(1) space b. O(N + M) time, O(N + M) space c. O(N + M) time, O(1) space d. O(N * M) time, O(N + M) space

c

What will be the time complexity of the following code? for(int i=0;i<n;i++){ i*=k; } a. O(n) b. O(k) c. O(logkn) d. O(lognk)

c

Which of the following is a superclass of every class in Java? a. ArrayList b. Abstract class c. Object class d. String

c

Which of the following is not an OOP concept in Java? a. polymorphism b. inheritance c. compilation d. encapsulation e. abstraction

c

Which of these is not a correct statement? a. Every class containing abstract method must be declared abstract b. Abstract class defines only the structure of the class not its implementation c. Abstract class can be initiated by new operator d. Abstract class can be inherited

c

Which of these is supported by method overriding in Java? a. Abstraction b. Encapsulation c. Polymorphism d. None of these

c

Which of these keywords is used to manually throw an exception? a. try b. finally c. throw d. catch

c

Find the output of the following code. int ++a = 100; System.out.println(++a); a. 100 b. 101 c. 102 d. Compile error

d

How many objects will be created in the following? String a = new String("Interviewbit"); String b = new String("Interviewbit"); Strinc c = "Interviewbit"; String d = "Interviewbit"; a. 1 b. 2 c. 3 d. 4

d

Identify the infinite loop. a. for(; ;) b. for(int i = 0; i < 1; i--) c. for(int i = 0; ;i++) d. All of these

d

Identify the output of the following program. String str = "Hellow"; System.out.println(str.indexOf('t)); a. 0 b. 1 c. false d. -1

d

What is the header (prototype) of the default constructor for the following class? public class Question{} a. Question() b. Question(void) c. public Question(void) d. public Question()

d

What is the output of the following code? public class Solution{ public static void main(String args[]){ int i; for(i = 1; i < 6; i++){ if(i > 3) continue; } System.out.println(i); } } a. 3 b. 4 c. 5 d. 6

d

What is the output of the following java program? public class Outer { public static int temp1 = 1; private static int temp2 = 2; public int temp3 = 3; private int temp4 = 4; public static class Inner { private static int temp5 = 5; private static int getSum() { return (temp1 + temp2 + temp3 + temp4 + temp5); } } public static void main(String[] args) { Outer.Inner obj = new Outer.Inner(); System.out.println(obj.getSum()); } } a. 15 b. 9 c. 5 d. compilation error

d

What is the output of the following program? class Derived { protected final void getDetails() { System.out.println("Derived class"); } } public class Test extends Derived{ protected final void getDetails() { System.out.println("Test class"); } public static void main(String[] args) { Derived obj = new Derived(); obj.getDetails(); } } a. Derived class b. Test class c. runtime error d. compilation error

d

What is the output of the following program? class Derived { public void getDetails() { System.out.println("Derived class"); } } public class Test extends Derived{ protected void getDetails() { System.out.println("Test class"); } public static void main(String[] args) { Derived obj = new Test(); // line xyz obj.getDetails(); } } a. Test class b. Compilation error due to line xyz c. Derived class d. compilation error due to access modifier

d

What is the output of the following program? public class Test{ public int getData() //getdata() 1 { return 0; } public long getData() //getdata 2 { return 1; } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.getData()); } } a. 1 b. 0 c. runtime error d. compilation error

d

What is the time complexity of the following code: int a = 0, i = N; while (i > 0) { a += i; i /= 2; } a. O(N) b. O(Sqrt(N)) c. O(N / 2) d. O(log N)

d

What is the time complexity of the following code: int a = 0; for (i = 0; i < N; i++) { for (j = N; j > i; j--) { a = a + i + j; } } a. O(N) b. O(N*log(N)) c. O(N * Sqrt(N)) d. O(N*N)

d

What will be the output of the following Java code snippet? import java.util.*; class Arraylists { public static void main(String args[]) { ArrayLists obj = new ArrayLists(); obj.add("A"); obj.add("B"); obj.add("C"); obj.add(1, "D"); System.out.println(obj); } } a. [A, D, C] b. [A, B, C] c. [A, B, C, D] d. [A, D, B, C]

d

What will be the output of the following Java code? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } a. 2 2 b. 3 3 c. runtime error d. compilation error

d

What will be the output of the following Java program? class Abc { public static void main(String[]args) { String[] elements = { "for", "tea", "too" }; String first = (elements.length > 0) ? elements[0]: null; } } a. Compilation error b. An exception is thrown at run time c. The variable first is set to null d. The variable first is set to elements[0]

d


Ensembles d'études connexes

Chapter 18.3 Formed elements in the blood

View Set

Human Resources Management Final

View Set

Chemical Reactions and Stoichiometry

View Set

Life and Health - Chapter 4 Quiz - Life Policy Provisions and Options

View Set

Intro to Philosophy- ALL TEST QUESTIONS- Brother David

View Set