Font size
WorksheetsFundamentalls of java
Total questions: 122
Worksheet time: 1hrs 19mins
what is size of int in Java?
2 bytes
4 bytes
6 bytes
8 bytes
what is size of long int in Java?
2 bytes
4 bytes
6 bytes
8 bytes
what is size of char in Java?
2 bytes
4 bytes
6 bytes
1 bytes
String in a Java is primitive (built-in) data type or class?
data type
class
What size will be required to store value 324.98 in Java?
2
3
6
8
Which of these can not be used for a variable name in Java and any other programming languages?
identifier
keyword
identifier & keyword
none of the mentioned
Import java.lang.*;
class Rectangle
{
int l,b,a;
a=l*b;
void fun()
{
l=10;
b=20;
system.out.println("Area = ", a);
}
}
Identify errors in above program segment.
Error in import line and Error in println line both
Error in import line
Error in println line
No error at all
Which of the following rule is INCORRECT in Java?
Class should not be end with semi-colon
Constants to be written in CAPITAL letters
main() should be inside the class
We can write Java program without using class
What is the output of the following?
int x = 9;
if (x>9)
System.out.println("Hello");
else
System.out.println("World");
Hello
World
Syntax Error
Compilation Error
What is the output of the following:
for(int x=1 ; x<5 ; x++){
System.out.print(x);
}
1234
01234
012345
12345
What is the output of the following:
String myWord = "FUN";
String anotherWord = myWord.toLowerCase();
System.out.println(myWord);
fun
Fun
FUN
"fun"
What is the output of the following:
int y = 5;
do{
System.out.println("This is not the output!");
y++;
}while(y>5);
6
Compilation error
This is not the output!
(printed 5 times)
This is not the output!
int x = 25;
if(5*5)
System.out.println("The answer is 25!");
else
Sytem.out.println("The answer is zero");
true
The answer is 25!
The answer is zero
Compilation Error
What is the output of the following?
boolean yes=true, no=false;
if (!yes||no)
System.out.println("maybe");
else
System.out.println("yes!");
no
maybe
Compilation Error
yes!
What is the output of the following?
String a = "I will pass the exams.";
String b = " I will pass the exams. ";
if (a.equals(b))
System.out.println("fail");
else
System.out.println(b.substring(9,12));
pass
fail
Compilation Error
I will pass the exams.
What is the output of the following?
String a = "Are you ready kids?";
String b = "Aye! Aye! Captain!";
System.out.println(a.concat(b));
Are you ready kids? Aye! Aye! Captain!
Compilation Error
Are you ready kids?Aye! Aye! Captain!
Aye! Aye! Captain!
What is the output of the following code?
String a = "19";
String b = "75";
System.out.println(a+" "+b);
19 75
94
Compilation Error
1975
What method of class String is used to remove white spaces at the beginning and end of String objects?
remove()
trim()
concat()
Substring()
In Java, lists and arrays are the same thing
True
False
If data members are private, what can we do to access them from the class object?
Create public member functions to access those data members
Create private member functions to access those data members
Create protected member functions to access those data members
Private data members can never be accessed from outside the class
Which feature can be implemented using encapsulation?
Inheritance
Abstraction
Polymorphism
Overloading
Which inheritance in java programming is not supported
Multiple inheritance
hierarchal inheritance
Multilevel inheritance
Single inheritance
What is subclass in java?
A subclass is a class that extends another class
A subclass is a class declared inside a class
Both above.
None of the above.
If class B is subclassed from class A then which is the correct syntax
class B:A{}
class B extends A{}
class B extends class A{}
class B implements A{}
Find which of the following uses encapsulation?
void main(){
int a;
void fun() { int a=10;
System.out.println(a);
fun(); }
class student{
int a;
public int b; };
class student {
int a;
public void disp(){
System.out.println(a);
} }
static topper() {
char name[10];
public int marks; }
Which of this keyword must be used to inherit a class?
super
this
extent
extends
Java is a (a) language
How many class files will be created for the following code?
class A { .... }
class B { .... }
public class C
{
public static void main(String args[])
{
...... } }
Note: Assume that A,B and C are in same file TestClass.java
1
2
3
0
The keyword "method" is an another name for?
function
variable
class
object
What will be the Output o the following code?
class Sample{
public static void main(String args[])
{
int len = args.length;
System.out.println(len);
}
}
Compilation Error
Runtime Error
The program compiles and executes successfully but prints nothing
The program compiles and executes successfully and prints 0
What will be the output of the following Java program?
class Output {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c); }
}
324
323
244
234
344
What will be the output of the following Java code?
class operators {
public static void main(String args[]) {
int x = 8;
System.out.println(++x * 3 + " " + x); }
24 8
27 9
24 9
27 8
What will be the output of the following Java code?
class Relational_operator {
public static void main(String args[]) {
int var1 = 5;
int var2 = 6;
System.out.print(var1 > var2); }
}
5
6
FALSE
TRUE
What will be the output of the following Java program?
class booloperators {
public static void main(String args[]) {
boolean var1 = true;
boolean var2 = false;
System.out.println((var1 & var2));
}
}
True
False
1
0
What will be the output of the following Java program?
class squareroot {
public static void main(String args[]) {
double a, b;
a = 3.0;
b = 4.0;
double c = Math.sqrt(a * a + b * b);
System.out.println(c); } }
compilation error
25
7
5
What is the result of the following statement
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);
} }
3
5
6
2
Which of the following is/ are feature(s) of OOP?
Classes and Objects
Data abstraction
Data Encapsulation
Inheritance and Polumorphism
Writing a method with the same name but the different signature is called as?
Method overriding
Method overloading
Method chaining
Method encapsulation
this keyword (self in python) is used to refer?
Calling method
Calling data member
Calling object
None of these
Creating a new class with some other class is called as?
Inheritance
Polymorphism
Binding
Encapsulation
Which of the following is not a valid Java collection
HashMap
Vector
ArrayList
LinkedListHashMap
To use foreach loop on a particular collection, collection should implement?
serializable
iterable
closeable
enumerable
Pick the correct way(s) of declaring an array in Java?
int[] x = new int[5]
int[] x = {1,2,3,4}
int[] x = new int[]{1,2,3,4}
int x[5] = {1,2,3,4,5}
Which of the following option leads to the portability and security of Java?
Bytecode is executed by JVM
The applet makes the Java code secure and portable
Use of exception handling
Dynamic binding between objects
_____ is used to find and fix bugs in the Java programs.
JVM
JRE
JDK
JDB
Which of the following is a valid declaration of a char?
char ch = '\utea';
char ca = 'tea';
char cr = \u0223;
char cc = '\itea';
What does the expression float a = 35 / 0 return?
0
Not a number
Infinity
Run time exception
Which of the following for loop declaration is not valid?
for ( int i = 99; i >= 0; i / 9 )
for ( int i = 7; i <= 77; i += 7 )
for ( int i = 20; i >= 2; - -i )
for ( int i = 2; i <= 20; i = 2* i )
When does method overloading is determined?
At run time
At compile time
At coding time
At execution time
Which of this keyword must be used to inherit a class?
super
this
extend
extends
To print "Hello, world" on the monitor, use the following Java statement:
System.out.println("Hello, world");
System Print = "Hello, world";
SystemOutPrintln('Hello, world');
system.out.println{Hello, world};
Operator used to access the members of a class /package
(.) Dot
(,) Comma
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);
[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
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 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);
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) );
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]
ar, given the following declaration:int[] ar = {2, 4, 6, 8 }0, 1, 2, 31, 2, 3, 40, 2, 4, 6For the array:
int stats[3];
What is the range of the index?
0 to 3
0 to 2
1 to 3
0 to 4
int [] nums = {2, 3, 5, 8, 9, 11};
How would you access the fourth element in nums?
nums[4]
nums[3]
nums(4)
nums(3)
a = {0, 2, 3, 4} and v = 1?
List all parameters that exist in this program segment:-
double point, String student
point, student
name, mark
name[i], mark[i]
Identify the error that exist in this program segment:-
An array data type cannot be use as argument.
The argument was not in the right sequence with the parameter.
The variable name in parameter not as same with the argument.
Program consist 0 error.
Identify the object that used to call a method.
Status
name[i], mark[i]
mtd
void
Fix the error by replacing with the correct java statement.
void Status(String student, double point)
void Status ( name[i], mark[i] )
int Status(String student, double point)
void Status ( String name[i], double mark[i] )
Create a method name Bonus that will read a basic payment and an hour variable as parameter to calculate an extra salary.
Identify the error that exist in this method program:-
Wrong use of return method type
variable extra was unknown.
doble is not one of the data type
A wrong use of hour data type.
Identify the error that exist in this method program:-
Wrong use of return method type
variable extra was unknown.
doble is not one of the data type
A wrong use of hour data type.
Identify the error that exist in this method program:-
Wrong use of return method type
variable extra was unknown.
All of the parameters data type was not stated.
missing { } in if statement.
A variable that holds words
String
boolean
int
float
A dictionary
A variable that holds true or false
String
boolean
int
float
Alex Trebek
-Contains no duplicate elements.
-Can contain at most one Null value.
-Elements are not key-value pairs.
-Accessing an element can be almost as fast as performing a similar operation on an array.
Which of these classes provides the specified features?
LinkedList
TreeMap
HashSet
LinkedHashMap
HashMap
Which of the following statement about Set is true?
A HashSet cannot have a null value.
Set allows duplicate values.
Set allows null values but only single occurrence.
Set extends the Java.util.collection interface.
-Entries are organized as key/value pairs.
-Duplicate entries replace old entries.
-Entries are sorted using a Comparator or the Comparable interface.
Which interface of the java.util package offers the specified behavior?
List
Map
Set
SortedSet
SortedMap
{1=C, 2=JavaEE, 4=Python, 3=PHP}
{1=C, 2=JavaSE, 3=Python, 4=PHP}
{1=C, 2=JavaEE, 3=Python, 4=PHP}
{1=C, 2=JavaSE,2=JavaEE, 3=Python, 4=PHP}
NullPointerException
{null=null, a=null, b=JavaSE, c=PHP, d=Python}
{a=null, b=JavaSE, c=PHP, d=Python}
{b=JavaSE, c=PHP, d=Python}
Compilation Fails
4
5
6
12
-Entries are not organized as key/value pairs.
-Duplicate entries are rejected.
Which interface of the java.util package offers the specified behavior?
List
Map
Set
None of the above
[Computer, Network, Networks, Programming, Security]
[Computer, Programming, Network, Networks, Programming, Security]
[Computer, Programming, Computer, Networks, Network, Security]
[Computer, Networks, Network, Programming, Security]
Which of the following is/are correct statements?
HashSet class implements the Set interface
HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.
HashSet permits to have a single null value.
HashMap does not allow duplicate keys however it allows to have duplicate values.
HashMap and HashSet are threadsafe
Which of the following statement(s) is/are TRUE?
Both HashMap and HashSet are not synchronized.
HashMap does not allow duplicate keys however it allows to have duplicate values.
HashMap permits single null key and any number of null values.
Both HashMap and HashSet are synchronized.
{"Computer", "Programming","Networks","null"}
{"Computer", "Programming","Networks",null,"null"}
Line 11 : Genetates NullPointer Exception
Line12: Generates NullPointerException
Which of the following statement(s) is/are FALSE?
TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
Objects in a TreeSet are stored in a sorted and ascending order.
TreeSet does not allow to insert Heterogeneous objects. It will throw classCastException at Runtime if trying to add hetrogeneous objects.
Comparator is note provided for TreeSet.
Which of the following is/are FALSE about HashMap in java?
It provides the basic implementation of Map interface of Java.
HashMap doesn’t allow duplicate keys but allows duplicate values.
HashMap allows null key also but only once and multiple null values.
Unsynchronized.
Synchronized
Which of the following is/ are True ?
Set is a group of ordered objects without duplicates
Map is essentially a set of (key,value) pairs with unique keys, supporting a lookup operation to find the value associated with a given key
List is a sequence of objects, with a distinguished first object, and in which each object has a unique successor.
Bag is a group of unordered objects allowing duplicates
Which statement(s) are false about maps in java?
A Map cannot contain duplicate keys and each key can map to at most one value.
A Map is an object that maps keys to values, or is a collection of attribute-value pairs.
A Map cannot contain duplicate keys and each key can map to at more than one value.
A Map is an object that maps functions to values, or is a collection of attribute-value pairs.
Which statement(s) are false about maps in java?
A Map cannot contain duplicate keys and each key can map to at most one value.
A Map is an object that maps keys to values, or is a collection of attribute-value pairs.
A Map cannot contain duplicate keys and each key can map to at more than one value.
A Map is an object that maps functions to values, or is a collection of attribute-value pairs.
List listNames = new ArrayList();
listNames.add("Tom");
listNames.add("Mary");
listNames.add("Peter");
String name2 = (String) listNames.get(1);
System.out.println(name2);
What is the output of the above code
Tom
Mary
Peter
raises Exception
Which of the following statement is not True about Generics in java?
Generics are features of the Java programming language that allow programmers to write parameterized code.
With generics, the compiler can not detect violations at compile time, thus eliminating potential bugs.
Due to the demand of safer code, generics were added to the Java programming language.
generics allow you to write generic code
Stronger type checks at compile time
When do we use sets in java program?
To allow null and duplicate elements
You want to store elements distinctly without duplication, or unique elements.
to allow t contain duplicate keys and each key can map to at most one value
To perform lookups by keys
Describe method.
A component of a program in a big program.
A program to perform a specific task.
A component of a program written to perform a specific task.
A variable that use to read an input.
This is a general structure a method.
<Returnvalue type> <Method Name> (argument / s)
<Modifier> < type> <Method Name> (parameter / s)
public static void main (String[ ] args)
<Method Name> (parameter)
Fix the syntax error in Line 8.
Cylinder sc = new System.in( );
Scanner sc = new Scanner (System.in);
Cylinder Volume = new Volume( );
Cylinder call = new Cylinder( );
double quiz1 = 0.0;
When programming, you have to use Data Types. Which of the following refers to interfaces, classes, and arrays?
Primitive
Non-Primitive
Arrays
Boolean
Which one is a Java valid variable name?
Sum Grades
static
public
FindAverage
The output of the following code is:
int[] array = {2,4,6,8,10};
for (int i=0; i < array.length;i++){
System.out.println(array[2]);
}
2
4
6
8
10
2,4,6,8,10
6
6
6
6
6
Nothing
3 + 6 / 2 * 3 - 1 + 2 =
14.5
13
5
Which of the following pairs of declarations will cause an error message?
I. double x = 14.7; int y = x;
II. double x = 14.7; int y = (int) x;
III. int x = 14; double y = x;
None
I only
II only
III only
I and III only
Refer to the following code fragment:
double answer = 13 / 5;
System.out.println("13 / 5 = " + answer);
The output is:
13 / 5 = 2.0
The programmer intends the output to be:
13 / 5 = 2.6
Which of the following replacements for the first line of code will NOT fix the problem?
double answer = (double) 13 / 5;
double answer = 13 / (double) 5;
double answer = 13.0 / 5;
double answer = 13 / 5.0;
double answer = (double) (13 / 5);
Which of the following will evaluate to true only if boolean expressions
A, B, and C are all false?
!A && !(B && !C)
!A || !B || !C
!(A || B || C)
!(A && B && C)
!A || !(B || !C)
Name the data type: '3'
int
char
String
None of the above
How would you declare a variable storing a person's name?
string name = "Jeff";
name String = "Jeff";
String name = Jeff;
String name = "Jeff";
What prints out "I love Java" successfully?
System.out.println(I love Java);
Systemoutprintln("I love Java);
System.out.println("I love" + " Java");
System.out.println("I love Java")
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
What will be the output of the program?
true
false
Compilation fails
An exception is thrown at runtime
What will be the output of the program?
5 3
6 3
6 4
5 2
What will be the output of the program?
5 3
8 3
8 2
8 5
What will be the output of the program?
0
7
8
14
What will be the output of the program?
ok
ok dokey
dokey
No output is produced
