Font size
WorksheetsBB_QUIZ
Total questions: 93
Worksheet time: 1hrs 6mins
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
Predict the output?
public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
10 20 30 40 50
Compiler Error
10 20 30 40
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
0
0
garbage value
garbage value
Compiler Error
Exception
public class Main {
public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Compiler Error
0
1 2
3 4 5
6 7 8 9
0
0 0
0 0 0
0 0 0 0
9
7 8
4 5 6
0 1 2 3
Which of the following is the correct way of importing an entire package ‘pkg’?
import pkg.
Import pkg.
import pkg.*
Import pkg.*
Which of the following is/are advantages of packages?
Packages avoid name clashes
Classes, even though they are visible outside their package, can have fields visible to packages only
We can have hidden classes that are used by the packages, but not visible outside.
All of the above
Which of these is a mechanism for naming and visibility control of a class and its content?
Object
Packages
Interfaces
None of the above
Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?
Public
Protected
No Modifier
All of the mentioned
Arrays in Java are dynamically allocated using the . . . . operator.
create
dynamic
arrayList
new
Which of the following statements correctly declares a two-dimensional integer array?
int Matrix[ ] = new int[5,4];
int Matrix[ ]; Matrix = new int[5,4];
int Matrix[ ][ ] = new int[5][4];
int Matrix[ ][ ] = int[5][4];
What is the ArrayList nums if it is initially [5, 3, 1] and the following code is executed?
nums.add(6);
nums.add(0, 4);
nums.remove(1);
[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 nums ArrayList?
nums.size()-1
nums.length()-1
nums.size()
nums.length
Which statement below is the correct way to retrieve the first element in the nums ArrayList?
nums.get(0)
nums[0]
nums(0)
nums[1]
Given the ArrayList nums with the values [3, 7, 6, 0], what code below is the proper way to change the 7 to be a 5?
nums.set(1, 5)
nums.set(7, 5)
nums.set(5, 2)
nums[2] = 5
Given the nums ArrayList with the values [5, 4, 3, 2], what statement below removes the value 3 from the list?
nums.remove(2)
nums.remove(3)
nums.get(2) = null
nums.remove(1)
Given the nums ArrayList with the values [1, 2, 3, 4, 6], what code below can be used to add a 5 between the 4 and 6 in the list?
nums.add(4, 5)
nums.add(5, 4)
nums.add(5, 5)
nums.add(5)
What will print when the following code executes?
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(7);
list.add(8);
list.add(9);
System.out.println(list.remove(0));
7
8
9
0
What is printed?
ArrayList<Integer> alist;
alist = new ArrayList<Integer>();
alist.add(1);
alist.add(2);
alist.remove(1);
alist.add(1, 3);
alist.set(1, 4);
alist.add(5);
System.out.println(alist);
[1, 4, 5]
[1, 4, 3, 5]
[2, 4, 5]
[2, 4, 3, 5]
What is printed?
ArrayList<Double> vals;
vals = new ArrayList<Double>();
vals.add(3.5);
vals.add(4.5);
vals.add(5.5);
vals.set(0, 2.5) ;
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 String values?
ArrayList<String> name;
name = new ArrayList<String>();
ArrayList name;
name = new ArrayList<String>();
ArrayList<String> name;
name = ArrayList<String>();
String<ArrayList> name;
name = new String<ArrayList>();
What is printed after executing code on the nums ArrayList with the values [3, 2, 4, 5, 7]?
7
5
6
memory full because too many values added
What is the output of the following snippet?
Hello Java
Java Hello
Hello
String cannot be modified.
Which of these is not a method of String class?
charAt()
getChars()
getString()
toCharArray()
Strings in Java can be created using:
String
StringBuffer
StringBuilder
All of these
To which package does the string class belong to?
java.io
java.util
java.lang
javax.conn
Given this snippet , char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = new String(chars, 2, 3);
What is the value of s?
abc
cde
bc
def
byte ascii[] = {65, 66, 67};
String s1 = new String(ascii);
What is the value of s1?
656657
67
ABC
abc
toString() belongs to ------- class?
String
Scanner
Object
Main
char ch;
ch = "Hello".charAt(1);
What is stored in ch?
Null
He
H
e
What is the output of the code?
None
What is the result of the expression: "Hello".substring(1,4)
Hel
H
ell
llo
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 x = "10";
String y = "20";
String z = x + y;
System.out.println(z);
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
Choose most appropriate purpose of trim() method in String?
to remove white spaces
to get a substring of the string
to cut String at desired index
to remove extra white spaces from start and end of String
What is the return type of equalsIgnorecase() method?
int
String
char
boolean
What is the most appropriate way to check if two Strings are identical?
string1 == string2
string1.equals(string2)
string1.same(string2)
string1.equalsIgnorecase(string2)
(Multiple answer question)
Pick all NON-primitive datatypes from below :
String
int
primitive
Array
What is the correct relation between length and last-index of array?
last-index is always 1 more than the length
last-index is 1 less than the length (but not always)
last-index is always 1 less than the length
last-index is always equal to the length
What is word.length()?
What is word.charAt(7) ?
What is word.startsWith("A few") ?
What is word.endsWith("man") ?
String word = "A few good men";
What is word.indexOf(“f”)?
2
0
1
3
What is word.substring(3, 7) ?
A programming mechanism that binds together code and the data it manipulates, and that keeps both safe from outside interference and misuse.
Polymorphism
Inheritance
Encapsulation
Object-Oriented Programming
Allows us to consider complex ideas while ignoring irrelevant detail that would confuse us.
Abstraction
Encapsulation
Generalisation
Polymorphism
Object
As a Vehicle user, we know how to use it but its internal working are not known. In OOPs Concept, this principle is known as
Encapsulation
Inheritance
Abstraction
Polymorphism
Which component is used to compile,debug and execute the java programs ?
JRE
JIT
JDK
JVM
Which of the following language was developed as the first purely object programming language?
SmallTalk
C++
Kotlin
Java
Which two features of object-oriented programming are the same?
Abstraction and Polymorphism
Inheritance and Encapsulation
Encapsulation and Polymorphism
Encapsulation and Abstraction
Output?
The vehicle moves
The car moves
The code does not compile
None of the above
Output?
parent from parent
child from child
parent from child
child from parent
Java uses both compiler and interpreter
true
false
Which of the following concepts of OOPs means exposing only necessary information and hiding the background details?
Encapsulation
Abstraction
Data hiding
Data binding
What is a HashMap in Java used for?
To store data in key-value pairs.
To perform mathematical calculations.
To manage threads.
To handle exceptions.
Which is the superclass of Error class in Java?
Throwable
Exception
Runtine Exception
None of the above
23
Hi this is test 23
22
compiler Error
9.797
5.830
7.211
Compilation Error
Choose the correct output...
12.4 12.4
12 12
14 12.4
12.4 12
Find the output of the given code
Hi this is test
6
2
compiler error
Hi this is test
2
Find the output of the given code
65621
390689
Compiler error
190332
What is the output of the given code..?
Garbage value
100
200
Compiler error
What is the output of the below code..?
true true
false false
true false
false true
Find the output of the given code....
0
1
2
0 1 2
Find the correct Java statement(s)...
1 and 2
2 and 3
3 and 4
All statements are correct
a = 3 s = Blue
a = 5 s = Yellow
a = 3 s = Yellow
a = 5 s = Blue
What is the output of the code given...?
0 1 2
0 2 4
0 0 4
0 1 4
No divide by Zero Error….
Its Working
Compilation Error
Its Working No divide by Zero Error…
1 2
2 1
Runtime Error
Compilation Error
6 6
6.4 6.4
6.4 6
4 6.4
Find the correct output of the given code....
===============
#################
&&&&&&&&&
&&&&&&&&&&&
===============
#################
&&&&&&&&&&
What is the advantage of Exception Handling
To avoid abnormal termination of a program
To find out errors
To Debug program
None of these
What is the parent class of All Exceptions in JAVA
Throw
Exception
Error
Bug
Which of the following key word is optional in Exception handling program
try
catch
finally
throw
What is the purpose of 'throw' keyword
To Raise an exception explicityly
To Raise an exception implicityly
To create user defined exceptions
To create custom exceptions
Exception classes belongs to following package
import java.io.*
import java.lang.*
import java.util.*
import java.lang.Exception.*
How to create our own exception
using 'throw' keyword
using 'throws' keyword
using 'finally' keyword
using 'throwable' keyword
Choose an exception if attempt to divide number by zero
int number = 89 / 0;
System.out.println("The answer is " + number);
ArithmeticException
NullPointerException
NumberFormatException
ArrayIndexOutOfBoundException
What will it print?
1
2
3
30
1
2
30
1
2
3
11
1
2
11
What will this program print?
Nothing, there is an error
There was a ValueError, please try again
num1num2
num2num1
public class Foo
{
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.
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
Compiler Error
none
Choose the CORRECT mechanism of exception handling.
try block --> catch block --> finally block
try block --> catch block --> throws exception
try block --> catch block --> finally --> throws exception
try block --> throws exception--> catch block --> finally block
What is the output?
The code will lead to a compilation error as declaration of the display method has been provided in two interface.
The code will lead to a compilation error due to public modifier while declaring the display method.
The code will compile and execute successfully showing the output Welcome to Examveda.
The code will lead to a compilation error as the display method is not declared as abstract.
None of these
Exception is a class/interface/abstract class/other?
Class
Interface
Abstract class
Other
1. class main_class
2. {
3. public static void main(String args[])
4. {
5. int y = 10;
6. if (y == 10)
7. {
8. int y = 11;
9. System.out.println(y);
10. }
11. }
12. }
Compilation error
10
11
Run time error
