5- Java main file
5- Java main file
A. Microsoft
B. Mozilla Corporation
C. Sun Microsystems
D. Amazon Inc.
View Answer
Ans : C
A. Eclipse
B. OAK
C. Netbean
D. D
View Answer
Ans : B
A. James Gosling
B. Larry Page
C. Bjarne Stroustrup
D. None of these
View Answer
Ans : A
Explanation: None
A. because the language was unsuccessful , so they created another version and changed its name to
java.
B. because the name was not relevant to the language they created
C. because there was another language called Oak
D. None of these
View Answer
Ans : C
Explanation: OAK was renamed to Java because there was another language called Oak
A. 25-05-1995
B. 27-05-1995
C. 29-05-1995
D. 28-05-1995
View Answer
Ans : B
A. Object Oriented
B. Procedural
C. Event Driven
D. None of these
View Answer
Ans : A
View Answer
Ans : B
Explanation: Basic aim of java was to solve the problem of connecting many household machines
together..
A. high-level
B. Middle-level
C. Low-level
D. None of the above
View Answer
Ans : A
View Answer
Ans : D
10. In which year, Sun Microsystems released much of its Java virtual machine?
A. 2005
B. 2006
C. 2007
D. 2008
View Answer
Ans : B
Explanation: On November 13, 2006, Sun released much of its Java virtual machine (JVM) as free and
open-source software (FOSS), under the terms of the GNU General Public License (GPL).
A. Internet development
B. Search Engine Development
C. Desktop Development
D. Operating System Development
View Answer
Ans : A
Explanation: After first public release java was targeted at Internet development.
12. First public implementation of java was _____________
A. Java 1.2
B. Java 1.0
C. Java 1.1
D. Java Premium 1.0
View Answer
Ans : B
13. Basic aim of java was to solve the problem of connecting many household machines together.
View Answer
Ans : D
Explanation: None
14. Sun Microsystems released the first public implementation as Java 1.0 in?
A. 1994
B. 1995
C. 1996
D. 1997
View Answer
Ans : C
Explanation: Sun Microsystems released the first public implementation as Java 1.0 in 1996.
View Answer
Ans : D
A. .Obj
B. Source Code
C. Bytecode
D. .Exe
View Answer
Ans : C
Explanation: Java Source Code is compiled into Bytecode.
A. Java 4.0
B. Java 8.0
C. Java 5.0
D. Java 6.0
View Answer
Ans : C
Explanation: Enums were introduced in Java 5.0. Enums restrict a variable to have one of only a few
predefined values. The values in this enumerated list are called enums.
3. Which of the following is used to interpret and execute Java Applet Classes hosted by HTML.
A. appletviewer
B. appletwatcher
C. appletshow
D. appletscreen
View Answer
Ans : A
Explanation: appletviewer is used to interpret and execute Java Applet Classes hosted by HTML.
A. javahelp
B. javamanual
C. javadoc
D. None of these
View Answer
Ans : C
Explanation: HTML based Java Documentary help can be accessed using javadoc.
A. /**
B. //
C. /*
D. None of these
View Answer
Ans : B
Explanation: Single-line comments start with two forward slashes ( // ). Any text between // and the
end of the line is ignored by Java
A. javac filename.java
B. java filename.java
C. javac filename
D. java filename
View Answer
Ans : A
Explanation: Type javac filename.java and press enter to compile your code. If there are no errors in
your code, the command prompt will take you to the next line.
View Answer
Ans : D
A. True
B. False
C. Depends On Complier
D. May be true or false
View Answer
Ans : A
Explanation: Case Sensitivity : Java is case sensitive, which means identifier Hello and hello would
have different meaning in Java.
A. For all class names the first letter should be in Upper Case.
B. All method names should start with a Lower Case letter.
C. Name of the program file should exactly match the class name.
D. All of the above
View Answer
Ans : D
A. Variables
B. identifiers
C. Access Modifiers
D. Java Modifiers
View Answer
Ans : B
Explanation: All Java components require names. Names used for classes, variables, and methods
are called identifiers.
A. Access Modifier
B. Non-access Modifiers
C. Both A and B
D. It's variable
View Answer
Ans : A
Ans : B
A. transient
B. instanceof
C. emun
D. strictfp
View Answer
Ans : C
A. assert
B. boolean
C. abstract
D. finalize
View Answer
Ans : D
Explanation: Final class can't be inherited, final method can't be overridden and final variable value
can't be changed. Finally is used to place important code, it will be executed whether exception is
handled or not. Finalize is used to perform clean up processing just before object is garbage
collected.
15. What is JRE?
View Answer
Ans : B
Explanation: Java Runtime Environment is an implementation of the Java Virtual Machine which
executes Java programs. It provides the minimum requirements for executing a Java application.
A. int
B. byte
C. short
D. long
View Answer
Ans : B
A. byte
B. enum
C. short
D. int
View Answer
Ans : B
3. Integer Data type does not include following primitive data type ____________.
A. long
B. byte
C. short
D. double
View Answer
Ans : D
4. Which of the following data types comes under floating data types ?
A. int
B. double
C. long
D. byte
View Answer
Ans : B
A. Digit
B. Letter
C. Special Character
D. String
View Answer
A. -128 to 128
B. -127 to 127
C. -127 to 128
D. -128 to 127
View Answer
Ans : D
A. 1 Bytes
B. 2 Bytes
C. 4 Bytes
D. 8 Bytes
View Answer
Ans : C
A. boolean
B. int
C. float
D. long
View Answer
Ans : D
A. -32768
B. -32767
C. 32768
D. 32767
View Answer
Ans : A
A. TRUE
B. FALSE
C. null
D. garbage
View Answer
Ans : B
class Main {
int t;
System.out.println(t);
A. 0
B. garbage value
C. compiler error
D. runtime error
View Answer
Ans : C
Explanation: Unlike class members, local variables of methods must be assigned a value to before
they are accessed, or it is a compile error.
class Test {
for(int i = 0; 0; i++)
System.out.println(""Hello"");
break;
A. Hello
B. Empty Output
C. Compiler error
D. Runtime error
View Answer
Ans : C
Explanation: The error is in for loop where 0 is used in place of boolean value. Unlike C++, use of non
boolean variables in place of bool is not allowed
class mainclass {
if (var1)
System.out.println(var1);
else
System.out.println(var2);
}
A. 0
B. 1
C. TRUE
D. FALSE
View Answer
Ans : C
Explanation: True.
class LFC {
int a = object.intValue();
byte b = object.byteValue();
float d = object.floatValue();
double c = object.doubleValue();
System.out.println(a + b + c + d );
A. 8
B. 8.8
C. 8.800000095
D. 8
View Answer
Ans : C
Explanation: Arithmetic conversions are implicitly performed to cast the values to a common type.
The compiler first performs integer promotion. If the operands still have different types, then they
are converted to the type that appears highest in the hierarchy.
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. All statements are correct
View Answer
Ans : D
Explanation: Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point
number (a double in this case) is cast to an int, it simply loses the digits after the decimal. (2) and (4)
are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant
(leftmost) bits. (3) actually works, even though a cast is not necessary, because a long can store a
byte.
class average {
double result;
result = 0;
for (int i = 0; i<6; ++i)
System.out.print(result/6);
A. 16.34
B. 16.56666664
C. 16.46666667
D. 16.76666667
View Answer
Ans : C
Explanation: No Explanation.
class output {
double a, b,c;
a = 4.0/0;
b = 0/3.0;
c=0/0.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
A. Infinity
B. 0
C. NaN
D. all of the mentioned
View Answer
Ans : D
Explanation: For floating point literals, we have constant value to represent (10/0.0) infinity either
positive or negative and also have NaN (not a number for undefined like 0/0.0), but for the integral
type, we don't have any constant that's why we get an arithmetic exception.
class increment {
int g = 4;
System.out.print(++g * 8);
A. 32
B. 36
C. 40
D. 48
View Answer
Ans : C
Explanation: Operator ++ has more preference than *, thus g becomes 5 and when multiplied by 8
gives 40.
class area {
double r, pi, a;
r = 9.8;
pi = 3.14;
a = pi * r * r;
System.out.println(a);
A. 301.5656
B. 301
C. 301.56
D. 301.57
View Answer
Ans : A
Explanation: The output for the following code is 301.5656.
class increment {
int g = 6;
System.out.print(--g * 8);
A. 48
B. 40
C. 56
D. 44
View Answer
Ans : B
Explanation: Operator -- has more preference than *, thus g becomes 5 and when multiplied by 8
gives 40.
21. What is the range of short data type in Java?
A. -128 to 127
B. -32768 to 32767
C. -2147483648 to 2147483647
D. None of the mentioned
View Answer
Ans : B
Explanation: Short occupies 16 bits in memory. Its range is from -32768 to 32767.
22. Which of the following are legal lines of Java code? Â Â 1. int w = (int)888.8; Â Â 2. byte x =
(byte)100L; Â Â 3. long y = (byte)100; Â Â 4. byte z = (byte)100L;
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. All statements are correct
View Answer
Ans : D
Explanation: Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point
number (a double in this case) is cast to an int, it simply loses the digits after the decimal. (2) and (4)
are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant
(leftmost) bits. (3) actually works, even though a cast is not necessary, because a long can store a
byte.
23. An expression involving byte, int, and literal numbers is promoted to which of these?
A. int
B. byte
C. long
D. float
View Answer
Ans : A
Explanation: An expression involving bytes, ints, shorts, literal numbers, the entire expression is
promoted to int before any calculation is done.
24. Which of these literals can be contained in float data type variable?
A. -1.7e+308
B. -3.4E+38
C. +1.7e+308
D. -3.4E+50
View Answer
Ans : B
25. Which data type value is returned by all transcendental math functions?
A. int
B. float
C. double
D. long
View Answer
Ans : C
Explanation: None.
26. Which of these coding types is used for data type characters in Java?
A. ASCII
B. ISO-LATIN-1
C. UNICODE
D. None of the mentioned
View Answer
Ans : C
Explanation: Unicode defines fully international character set that can represent all the characters
found in all human languages. Its range is from 0 to 65536.
A. boolean b1 = 1;
B. boolean b2 = false;
C. boolean b3 = false;
D. boolean b4 = true
View Answer
Ans : C
class array_output {
array_variable[i] = 'i';
System.out.print(array_variable[i] + """" );
i++;
A. i i i i i
B. 0 1 2 3 4
C. i j k l m
D. None of the mentioned
View Answer
Ans : A
A. 0x99fffL
B. ABCDEFG
C. 0x99fffa
D. 99671246
View Answer
Ans : A
Explanation: Data type long literals are appended by an upper or lowercase L. 0x99fffL is
hexadecimal long literal.
A. Integer
B. Boolean
C. Character
D. Integer or Boolean
View Answer
Ans : D
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer) or
on booleans (and it returns a boolean).
1. A ________ provides us with named storage that our programs can manipulate.
A. data type
B. constants
C. operators
D. variable
View Answer
Ans : D
Explanation: A variable provides us with named storage that our programs can manipulate.
2. Each variable in Java has a specific type, which determines the size and layout of the variable's
memory.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, Each variable in Java has a specific type, which determines the size and layout of
the variable's memory.
3. To declare more than one variable of the specified type, we can use a __________ list.
A. colon-separated
B. bracket-separated
C. comma-separated
D. None of the above
View Answer
Ans : C
Explanation: To declare more than one variable of the specified type, you can use a comma-
separated list.
A. 2
B. 3
C. 4
D. 5
View Answer
Ans : B
Explanation: There are three kinds of variables in Java : Local variables, Instance variables and
Class/Static variables
A. methods
B. constructors
C. blocks
D. All of the above
View Answer
6. What is true about Instance Variables in java?
View Answer
Ans : D
Explanation: All of the above are true about Instance Variables in java
A. Static Variables
B. Instance Variables
C. Local Variable
D. Both A and B
View Answer
Ans : C
Explanation: There is no default value for local variables, so local variables should be declared and an
initial value should be assigned before the first use.
A. Object name
B. Class name
C. Function name
D. Can not say
View Answer
9. Which of the following is an Example of variable initialization?
A. int a, b, c;
B. int a = 10, b = 10;
C. int 10 = a;
D. None of the above
View Answer
Ans : B
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
A. Characters
B. Boolean
C. Numeric
D. Both Numeric & Characters
View Answer
Ans : D
Explanation: The operand of arithmetic operators can be any of numeric or character type, But not
boolean.
View Answer
Ans : A
Explanation: Modulus operator can be applied to both integers and floating point numbers..
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : A
A. Assignment operators can be used only with numeric and character data type
B. Assignment operators are more efficiently implemented by Java run-time system than their
equivalent long forms
C. Assignment operators run faster than their equivalent long forms
D. None of the mentioned
View Answer
Ans : D
5. Can 8 byte long data type be automatically type cast to 4 byte float data type?
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : A
Explanation: Both data types have different memory representation that is why 8-byte integral data
type can be stored to 4-byte floating point data type.
6 - 2 + 10 % 4 + 7
A. 14
B. 12
C. 13
D. 10
View Answer
Ans : C
Explanation: Output of the expression is 13.
A. ( )
B. { }
C. Both A & B
D. None of these
View Answer
View Answer
Ans : C
Explanation: Logical AND(&&) and Logical OR(||) combine two boolean values.
9. Which of the following is the correct expression that evaluates to true if the number x is between
1 and 100 or the number is negative?
View Answer
Ans : B
Explanation: No explanation.
10. Select from among the following character escape code which is not available in Java.
A. \\
B. \v
C. \a
D. \t
View Answer
Ans : C
Explanation: No explanation.
class Main {
p.start();
void start()
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
a3[1] = 7;
return a3;
A. 12 15
B. 15 15
C. 3 7 5 3 7 5
D. 3 4 5 3 7 5
View Answer
Ans : B
Explanation: The reference variables a1 and a3 refer to the same long array object. When the [1]
element is updated in the fix() method, it is updating the array referred to by a1. The reference
variable a2 refers to the same array object. So Output: 3+7+5+"" ""3+7+5 Output: 15 15 Because
Numeric values will be added
class Main {
p.start();
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
b1 = true;
return b1;
A. true true
B. true false
C. false true
D. false false
View Answer
Ans : C
Explanation: The boolean b1 in the fix() method is a different boolean than the b1 in the start()
method. The b1 in the start() method is not updated by the fix() method.
class Main {
p.start();
}
void start()
String s1 = "s";
String s2 = fix(s1);
s1 = s1 + "st";
return "st";
A. s st
B. sst st
C. st s st
D. sst s st
View Answer
Ans : D
Explanation: When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both
refer to the same String object (with a value of ""s""). Fix()'s s1 is reassigned to a new object that is
created when the concatenation occurs (this second String object has a value of ""sst""). When the
program returns to start(), another String object is created, referred to by s2 and with a value of
""st"".
14. Which of the following will produce an answer that is closest in value to a double, d, while not
being greater than d?
A. (int)Math.min(d);
B. (int)Math.abs(d);
C. (int)Math.max(d);
D. (int)Math.floor(d);
View Answer
Ans : D
class Test {
int x = -4;
System.out.println(x>>1);
int y = 4;
System.out.println(y>>1);
View Answer
Ans : B
Explanation: No explantion.
16. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
A. 1, 2 & 3
B. 1 & 4
C. 1, 2, 3 & 4
D. 3 & 2
View Answer
Ans : C
class Main {
double var1 = 2 + 4;
int var3 = 2 + 4;
A. 0 1
B. 1 1
C. 1.5 1
D. 1.5 1.0
View Answer
Ans : C
Explanation: No Explanation.
class Main {
int x=20;
System.out.println(sup);
A. s
B. t
C. h
D. Compilation fails
View Answer
Ans : B
Explanation: This is an example of a nested ternary operator. The second evaluation (x < 22) is true,
so the ""t"" value is assigned to sup.
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
A. 7
B. 0
C. 14
D. 8
View Answer
Ans : C
Explanation: The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9.
The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The |
operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.
class increment
int g = 5;
System.out.print(++g * 8);
A. 44
B. 56
C. 48
D. 40
View Answer
Ans : C
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8
gives 32.
A. Integer
B. Boolean
C. Characters
D. Double
View Answer
Ans : B
Explanation: None.
22. Which of these is returned by "greater than", "less than" and "equal to" operators?
A. Integers
B. Floating - point numbers
C. Boolean
D. None of the mentioned
View Answer
Ans : C
Explanation: All relational operators return a boolean value ie. true and false.
A. &&
B. ==
C. ?:
D. +=
View Answer
Ans : D
Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean
logical operators. += is an arithmetic operator it can operate only on numeric values.
24. Which of these operators can skip evaluating right hand operand?
A. !
B. |
C. &
D. &&
View Answer
View Answer
Ans : D
Explanation: True and false are keywords, they are non numeric values which do not relate to zero
or non zero numbers. true and false are boolean values.
class Relational_operator
int var1 = 5;
int var2 = 6;
A. 1
B. 0
C. TRUE
D. FALSE
View Answer
Ans : D
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.
output: $ javac Relational_operator.java $ java Relational_operator false
27. What is the output of this program?
class ternary_operator
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
A. 0
B. 1
C. 3
D. -4
View Answer
Ans : C
class Output
{
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
A. 1
B. 2
C. Runtime error owing to division by zero in if condition
D. Unpredictable behavior of program
View Answer
Ans : B
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand
is false thus division by zero in if condition does not give an error. output: $ javac Output.java $ java
Output 2
class Output
{
boolean a = true;
boolean b = false;
boolean c = a ^ b;
System.out.println(!c);
A. 0
B. 1
C. FALSE
D. TRUE
View Answer
Ans : C
A. ()
B. ++
C. *
D. >>
View Answer
Ans : A
A. Integer
B. Floating - point numbers
C. Boolean
D. None of the mentioned
View Answer
Ans : C
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
A. 0
B. 1
C. 9
D. 8
View Answer
Ans : D
Explanation: None.
33. What is the order of precedence (highest to lowest) of following operators?
1. &
2. ^
3. ?:
A. 1 -> 2 -> 3
B. 2 -> 1 -> 3
C. 3 -> 2 -> 1
D. 2 -> 3 -> 1
View Answer
Ans : A
Explanation: None.
class operators
int var1 = 5;
int var2 = 6;
int var3;
System.out.print(var3);
A. 10
B. 11
C. 12
D. 56
View Answer
Ans : C
Explanation: Operator ++ has the highest precedence than / , * and +. var2 is incremented to 7 and
then used in expression, var3 = 7 * 5 / 7 + 7, gives 12. output: $ javac operators.java $ java operators
12
class Main
int x = 8;
A. 24 8
B. 24 9
C. 27 8
D. 27 9
View Answer
Ans : D
A. Character
B. Strings
C. Integer
D. Classes
View Answer
Ans : B
Explanation: Strings, which are widely used in Java programming, are a sequence of characters.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
View Answer
Ans : B
Explanation: The String class is immutable, so that once it is created a String object cannot be
changed.
A. string methods
B. class methods
C. object method
D. accessor methods
View Answer
Ans : D
Explanation: Methods used to obtain information about an object are known as accessor methods.
One accessor method that you can use with strings is the length() method, which returns the
number of characters contained in the string object.
A. printf()
B. format()
C. formatted()
D. Both A and B
View Answer
Ans : D
Explanation: You have printf() and format() methods to print output with formatted numbers. The
String class has an equivalent class method, format(), that returns a String object rather than a
PrintStream object.
A. int compareTo(Object o)
B. int compareTo(String anotherString)
C. char charAt(int index)
D. int compareToIgnoreCase(String str)
View Answer
Ans : C
Explanation: char charAt(int index) : Returns the character at the specified index.
7. Which method returns a String that represents the character sequence in the array specified?
View Answer
Ans : C
Explanation: boolean contentEquals(StringBuffer sb) : Returns true if and only if this String
represents the same sequence of characters as the specified StringBuffer.
View Answer
Ans : B
Explanation: boolean equals(Object anObject) : Compares this string to the specified object.
9. Which of these method of String class is used to obtain character at specified index?
A. char()
B. Charat()
C. charat()
D. charAt()
View Answer
Ans : D
Explanation: charAt() method of String class is used to obtain character at specified index.
10. Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: Yes, Whenever a subclass needs to refer to its immediate superclass, it can do so by use
of the keyword super.
1. A __________ statement allows us to execute a statement or group of statements multiple times.
A. array
B. loop
C. function
D. exception
View Answer
Ans : B
A. for
B. do-while
C. while
D. None of the above
View Answer
Ans : C
Explanation: A while loop statement in Java programming language repeatedly executes a target
statement as long as a given condition is true.
A. for
B. do-while
C. while
D. All of the above
View Answer
Ans : B
Explanation: A do...while loop is similar to a while loop, except that a do...while loop is guaranteed
to execute at least one time.
4. A for loop is useful when you know how many times a task is to be repeated.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, A for loop is useful when you know how many times a task is to be repeated.
5. Which statement causes the loop to immediately jump to the next iteration of the loop?
A. Exit
B. Break
C. Jump
D. Continue
View Answer
Ans : D
Explanation: The continue keyword can be used in any of the loop control structures. It causes the
loop to immediately jump to the next iteration of the loop.
6. What is the syntax of a continue is a single statement inside any loop?
A. Jump
B. Continue
C. Con
D. State-Continue
View Answer
Ans : B
Explanation: The syntax of a continue is a single statement inside any loop : continue;
A. For
B. While
C. Do-while
D. All of the above
View Answer
Ans : D
8. A BREAK statement inside a Loop like WHILE, FOR, DO WHILE and Enhanced-FOR causes the
program execution ___ Loop
A. Exit
B. Continuation with next iteration
C. Never exit
D. Can not say
View Answer
9. An enhanced FOR loop work with only Collection type data. Examples of Collection are ___.
View Answer
Ans : D
10. Break statement can be used to terminate a case in the switch statement?
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: Yes, break can be used to terminate a case in the switch statement
View Answer
Ans : A
Explanation: An array is a collection of similar type of elements which has contiguous memory
location.
A. Operator
B. Variable
C. index
D. Pointer
View Answer
Ans : C
A. Row
B. Column
C. Row and Column
D. None of the above
View Answer
Ans : A
A. Dynamic Memory
B. Static Memory
C. Both A and B
D. None of the above
View Answer
Ans : B
Explanation: Arrays in java are static lists that can store a certain kind of variables. Therefore these
arrays need to be initialized at the compile time.
View Answer
Ans : D
Explanation: int arr[] = int [5] is an incorrect array declaration because Operator new must be
succeeded by array type and array size.
A. -1
B. 0
C. 1
D. infinite
View Answer
Ans : B
View Answer
Ans : D
Explanation: int arr [] = {1, 2, 3}; is used to declare,construct, and initlaize an array becuase Option A
is wrong because it initializes an int array with String literals. Option B is wrong because it uses
something other than curly braces for the initialization. Option C is wrong because it provides initial
values for only one dimension, although the declared array is a two-dimensional array.
A. sizeof(array)
B. array.len
C. array.length
D. array.sizeof()
View Answer
Ans : C
A. Code Optimization
B. Random access
C. Size No-Limit
D. Both A and B
View Answer
Ans : A
Explanation: Code Optimization and Random access the following is advantage of java array.
A. Random
B. Sequential
C. Sequential & Random
D. Binary search
View Answer
Ans : B
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random
memory locations.
class Main
}
A. 10 20 30 40 50
B. Compiler Error
C. 10 20 30 40
D. None of the above
View Answer
Ans : A
Explanation: It is a simple program where an array is first created then traversed. The important
thing to note is, unlike C++, arrays are first class objects in Java. For example, in the following
program, size of array is accessed using length which is a member of arr[] object.
System.out.print(arr);
A. 0
B. value stored in arr[0].
C. 0
D. Class name@ hashcode in hexadecimal form
View Answer
Ans : D
Explanation: If we trying to print any reference variable internally, toString() will be called which is
implemented to return the String
class Main
array_variable[i] = i;
i++;
A. 0 2 4 6 8
B. 1 3 5 7 9
C. 0 1 2 3 4 5 6 7 8 9
D. 1 2 3 4 5 6 7 8 9 10
View Answer
Ans : A
Explanation: When an array is declared using new operator then all of its elements are initialized to 0
automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is
incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
class Main
int n = 6;
n = arr[arr[n] / 2];
System.out.print(n);
A. 3
B. 0
C. 6
D. 1
View Answer
Ans : A
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing
arr[arr[6]/2] i:e arr[3] = 3.
class Main
array_variable[i] = 'i';
}
A. 1 2 3 4 5 6 7 8 9 10
B. 0 1 2 3 4 5 6 7 8 9 10
C. i j k l m n o p q r
D. i i i i i i i i i i
View Answer
Ans : D
Explanation: No explantion.
class Test {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
A. 0 0
B. garbage value garbage value
C. Compiler Error
D. Exception
View Answer
Ans : C
Explanation: In Java, it is not allowed to put the size of the array in the declaration because an array
declaration specifies only the element type and the variable name. The size is specified when you
allocate space for the array. .
17. What will be output for the following code?
class Test {
System.out.println(arr[0]);
System.out.println(arr[1]);
A. 0 0
B. garbage value garbage value
C. Compiler Error
D. Exception
View Answer
Ans : A
Explanation: Java arrays are first class objects and all members of objects are initialized with default
values like o, null.
class array_output
int sum = 0;
for (int i = 0; i < 3; ++i)
System.out.print(sum / 5);
A. 8
B. 9
C. 10
D. 11
View Answer
Ans : B
Explanation: No explanation.
class Main
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}
A. Same
B. Not Same
C. Compiler error
D. None of the above
View Answer
Ans : B
Explanation: No explanation.
class Main
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
A. Same
B. Not Same
C. Compiler error
D. None of the above
View Answer
Ans : B
View Answer
Ans : A
Explanation: Array can be initialized using both new and comma separated expressions surrounded
by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
22. What is the type of variable "b" and "d" in the below snippet?
int a[], b;
int []c, d;
View Answer
Ans : D
Explanation: If [] is declared after variable it is applicable only to one variable. If [] is declared before
variable it is applicable to all the variables.
23. Which of these is necessary to What is the output of below snippet?specify at time of array
initialization?
A. ArrayIndexOutOfBoundsException
B. ArrayStoreException
C. Compilation Error
D. Code runs successfully
View Answer
Ans : B
Explanation: ArrayIndexOutOfBoundsException comes when code tries to access an invalid index for
a given array. ArrayStoreException comes when you have stored an element of type other than the
type of array..
A. Array.sort()
B. Arrays.sort()
C. Collection.sort()
D. System.sort()
View Answer
Ans : B
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and
searching). Array is not a valid class.
25. How to copy contents of array?
A. System.arrayCopy()
B. Array.copy()
C. Arrays.copy()
D. Collection.copy()
View Answer
Ans : A
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and
searching). Array is not a valid class.
Regular Expressions
1. Java provides the ______ package for pattern matching with regular expressions.
A. regex
B. java.regex
C. util.regex
D. java.util.regex
View Answer
Ans : D
Explanation: Java provides the java.util.regex package for pattern matching with regular expressions.
View Answer
Ans : C
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, Capturing groups are a way to treat multiple characters as a single unit.
A. $
B. ^
C. &
D. *
View Answer
Ans : B
View Answer
Ans : D
A. w
B. W
C. C
D. c
View Answer
Ans : B
View Answer
Ans : C
View Answer
Ans : D
Explanation: public int start() : Returns the start index of the previous match
A. Attempts to find the next subsequence of the input sequence that matches the pattern.
B. Attempts to match the input sequence, starting at the beginning of the region, against the
pattern.
C. Attempts to match the entire region against the pattern.
D. None of the above
View Answer
Ans : A
Explanation: public boolean find() : Attempts to find the next subsequence of the input sequence
that matches the pattern.
View Answer
Ans : A
Interfaces
A. interface
B. Interface
C. intf
D. Intf
View Answer
Ans : A
View Answer
Ans : D
Explanation: An interface can have both final variables and abstract methods.
A. Public
B. private
C. Protected
D. All of the mentioned
View Answer
Ans : A
Explanation: Access specifier of interface is either public or no specifier. When no access specifier is
used then default access specifier is used due to which interface is available only to other members
of the package in which it is declared, when declared public it can be used by any code.
View Answer
Ans : D
Explanation: No explanation.
5. Which of the following classes directly implement Set interface?
A. Vector
B. HashSet
C. HashTable
D. LinkedList
View Answer
Ans : B
6. Which of these can be used to fully abstract a class from its implementation?
A. Objects
B. Packages
C. Interfaces
D. None of the Mentioned
View Answer
Ans : C
Explanation: None.
interface calculate
int x;
x = item * item;
class Main
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
A. 0
B. 2
C. 4
D. None of the mentioned
View Answer
Ans : C
View Answer
Ans : A
Explanation: Concrete classes must implement all methods in an interface. Through interface
multiple inheritance is possible.
A. public static
B. private final
C. public final
D. static final
View Answer
Ans : D
Explanation: variable defined in an interface is implicitly final and static. They are usually written in
capital letters.
A. Method definition
B. Method declaration
C. Method declaration and definition
D. Method name
View Answer
Ans : B
A. Import
B. import
C. implements
D. Implements
View Answer
Ans : C
12. Which is the correct way to inherit and implement the interface?
View Answer
Ans : A
Explanation: Classes always implements an interface. An interface can extends another interface or
multiple interfaces. Hence, answer would be A.
Ans : A
Explanation: In java, an interface contains only abstract method that can be public and it does not
have any method implementation.
14. Which of the following is the correct way of implementing an interface salary by class manager?
View Answer
Ans : B
Explanation: No Explanation.
A. Interfaces are specified public if they are to be accessed by any code in the program
B. Interfaces specifies what class must do but not how it does
C. All variables in interface are implicitly final and static
D. All variables are static and methods are public if interface is defined pubic
View Answer
Ans : D
Explanation: All methods and variables are implicitly public if interface is declared public.
View Answer
Ans : A
Explanation: By default, interface contains abstract methods. The abstract methods need to be
implemented by concrete classes.
A. The concrete class implementing that method need not provide implementation of that method
B. Runtime exception is thrown
C. Compilation failure
D. Method not found exception is thrown
View Answer
Ans : C
Explanation: The methods of interfaces are always abstract. They provide only method definition.
Output: $ javac interfaces.java $ java interfaces 4
A. Compilation failure
B. Runtime Exception
C. The interface compiles successfully
D. The implementing class will throw exception
View Answer
Ans : A
A. Compilation failure
B. Runtime Exception
C. The JVM is not able to identify the correct variable
D. The interfaceName.variableName needs to be defined
View Answer
Ans : D
Explanation: The JVM needs to distinctly know which value of variable it needs to use. To avoid
confusion to the JVM interfaceName.variableName is mandatory.
20. Can "abstract" keyword be used with constructor, Initialization Block, Instance Initialization and
Static Initialization Block.
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : B
Explanation: No, Constructor, Static Initialization Block, Instance Initialization Block and variables
cannot be abstract.
Garbage Collection
1. What allows the programmer to destroy an object x?
A. x.finalize()
B. x.delete()
C. Runtime.getRuntime().gc()
D. Only the garbage collection system can destroy an object.
View Answer
Ans : D
Explanation: When an object is no longer referenced, it may be reclaimed by the garbage collector. If
an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the
object a last chance to clean up resources that would not otherwise be released. When a class is no
longer needed, it may be unloaded.
View Answer
Ans : D
Explanation: C See the note above on Islands of Isolation (An object is eligible for garbage collection
when no live thread can access it - even though there might be references to it). B is wrong. ""Never
again be used"" does not mean that there are no more references to the object. A is wrong. Even
though Java applications can run out of memory there another answer supplied that is more right.
View Answer
Ans : C
Explanation: Process restart is not a permanent fix to memory leak problem. The problem will
resurge again..
A. JVM
B. JProfiler
C. JConsole
D. Eclipse Profiler
View Answer
Ans : A
Explanation: Memory leak is like holding a strong reference to an object although it would never be
needed anymore. Objects that are reachable but not live are considered memory leaks. Various tools
help us to identify memory leaks.
A. All objects that are eligible for garbage collection will be garbage collected by the garbage
collector.
B. Objects from a class with the finalize() method overridden will never be garbage collected.
C. Objects with at least one reference will never be garbage collected.
D. Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
View Answer
Ans : D
Explanation: All objects are placed in the garbage collectible heap.
6. How to get prints of shared object memory maps or heap memory maps for a given process?
A. jmap
B. jvmmap
C. memorymap
D. memorypath
View Answer
Ans : A
View Answer
Ans : A
Explanation: JVM will be started with Xms amount of memory and will be able to use a maximum of
Xmx amount of memory. java -Xmx2048m -Xms256m.
A. Stack
B. Class
C. JVM
D. Heap
View Answer
Ans : C
Explanation: JVM is the super set which contains heap, stack, objects, pointers, etc.
A. Young space
B. JVM
C. Young or Old space depending on space availability
D. Old space
View Answer
Ans : A
Explanation: A new object is always created in young space. Once young space is full, a special young
collection is run where objects which have lived long enough are moved to old space and memory is
freed up in young space for new objects..
A. Sweep model
B. Mark and sweep model
C. Space management model
D. Cleanup model
View Answer
Ans : B
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase and the
sweep phase. I mark phase all the objects reachable by java threads, native handles and other root
sources are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps
between live objects and the gaps are marked free list used for allocating memory to new objects.
11. When is the B object, created in line 3, eligible for garbage collection?
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
A. after line 5
B. after line 6
C. after line 7
D. There is no way to be absolutely certain.
View Answer
Ans : D
Explanation: No explanation.
12. How many objects are eligible for garbage collection after execution of line ?
m1(); // Line
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : C
Explanation: Since t1 and t2 are local objects of m1() method, so they become eligible for garbage
collection after complete execution of method unless any of them is returned.
13. After line 11 runs, how many objects are eligible for garbage collection?
class X2
public X2 x;
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : C
Explanation: This is an example of the islands of isolated objects. By the time line 11 has run, the
objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of
them..
class Test
private Demo d;
void start()
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
demo = null;
A. After line 7
B. After line 8
C. After the start() method completes
D. When the instance running this code is made eligible for garbage collection.
View Answer
Ans : D
15. How many objects are eligible for garbage collection after execution of line 8?
t2 = t3; // line 8
}
static Test m1(Test temp)
return temp;
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : B
Explanation: By the time line 8 has executed, the only object without a reference is the one
generated i.e as a result of line 6. Remember that ""Java is strictly pass by value"" so the reference
variable t1 is not affected by the m1() method. We can check it using finalize() method. The
statement ""System.out.println(this.hashcode())"" in finalize() method print the object hashcode
value on which finalize() method is called,and then just compare the value with other objects
hashcode values created in main m
16. When is the Float object, created in line 3, eligible for garbage collection?
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
View Answer
Ans : C
Explanation: No explnation.
Exception Handling
View Answer
Ans : A
Explanation: Option A is correct. If the class specified in the catch clause does have subclasses, any
exception object that subclasses the specified class will be caught as well. Option B is wrong. The
error class is a subclass of Throwable and not Runtime Exception. Option C is wrong. You do not
catch this class of error. Option D is wrong. An exception can be thrown to the next method higher
up the call stack.
2. Which statement is true?
A. An Error that might be thrown in a method must be declared as thrown by that method, or be
handled within that method.
B. Multiple catch statements can catch the same class of exception more than once.
C. A try statement must have at least one corresponding catch block.
D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will
always start to execute.
View Answer
Ans : D
Explanation: A is wrong. A try statement can exist without catch, but it must have a finally
statement. B is wrong. A try statement executes a block. If a value is thrown and the try statement
has one or more catch clauses that can catch it, then control will be transferred to the first such
catch clause. If that catch block completes normally, then the try statement completes normally. C is
wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked
exceptions (java.lan
A. Run Time
B. Can Occur Any Time
C. Compilation Time
D. None of the mentioned
View Answer
Ans : A
A. finally
B. thrown
C. catch
D. try
View Answer
Ans : B
Explanation: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and
finally.
A. finally
B. throw
C. catch
D. try
View Answer
Ans : D
6. Which of these keywords must be used to handle the exception thrown by try block in some
rational manner?
A. finally
B. throw
C. catch
D. try
View Answer
Ans : C
Explanation: If an exception occurs within the try block, it is thrown and cached by catch block for
processing.
7. Which of these keywords is used to manually throw an exception?
A. finally
B. throw
C. catch
D. try
View Answer
Ans : B
8. Which of these is a super class of all errors and exceptions in the Java language?
A. Catchable
B. Throwable
C. RunTimeExceptions
D. None of the above
View Answer
Ans : B
Explanation: Throwable is a super class of all errors and exceptions in the Java language
A. java.file
B. java.lang
C. java.io
D. java.util
View Answer
Ans : B
Explanation: No explanation.
10. Which exception is thrown when divide by zero statement executes?
A. NumberFormatException
B. NullPointerException
C. ArithmeticException
D. None of these
View Answer
Ans : C
Java Lang
A. int a = Math.abs(-5);
B. int b = Math.abs(5.0);
C. int d = Math.abs(5L);
D. int c = Math.abs(5.5F);
View Answer
Ans : A
Explanation: The return value of the Math.abs() method is always the same as the type of the
parameter passed into that method. In the case of A, an integer is passed in and so the result is also
an integer which is fine for assignment to ""int a"". The values used in B, C & D respectively are a
double, a float and a long. The compiler will complain about a possible loss of precision if we try to
assign the results to an ""int"".
2. Which of these classes encapsulate runtime state of an object?
A. Class
B. Runtime
C. System
D. Cache
View Answer
Ans : A
A. Class
B. Integer
C. Array
D. Byte
View Answer
Ans : C
4. Which of the following will produce an answer that is closest in value to a double, d, while not
being greater than d?
A. (int)Math.abs(d);
B. (int)Math.max(d);
C. (int)Math.min(d);
D. (int)Math.floor(d);
View Answer
Ans : D
Explanation: The casting to an int is a smokescreen.
A. getClass()
B. WhoseObject()
C. Class()
D. WhoseClass()
View Answer
Ans : A
A. Void
B. Process
C. System
D. Runtime
View Answer
Ans : A
Explanation: The Void class has one field, TYPE, which holds a reference to the Class object for the
type void. I
A. void kill()
B. void destroy()
C. void terminate()
D. void exit()
View Answer
Ans : B
Explanation: Kills the subprocess. The subprocess represented by this Process object is forcibly
terminated
A. Void
B. Process
C. Runtime
D. System
View Answer
Ans : D
Explanation: Standard output variable "out" is defined in System class. out is usually used in print
statement i:e System.out.print().
A. Void
B. Process
C. Runtime
D. System
View Answer
Ans : B
Explanation: None.
10. Which of the following is method of System class is used to find how long a program takes to
execute?
A. currenttime()
B. currentTime()
C. currentTimeMillis()
D. currenttimeMillis()
View Answer
Ans : C
Explanation: None.
11. Which of these is a process of converting a simple data type into a class?
A. type casting
B. type conversion
C. type wrapping
D. None of the Mentioned
View Answer
Ans : B
Explanation: type conversion is a process of converting a simple data type into a class
12. Which of the following is method of wrapper Float for converting the value of an object into
byte?
A. Bytevalue()
B. byte bytevalue()
C. bytevalue()
D. Byte Bytevalue()
View Answer
Ans : B
Explanation: byte bytevalue() is method of wrapper Float for converting the value of an object into
byte
13. Which of these methods is used to check for infinitely large and small values?
A. isInfinite()
B. Isinfinite()
C. isNaN()
D. IsNaN()
View Answer
Ans : A
Explanation: isinfinite() method returns true is the value being tested is infinitely large or small in
magnitude.
14. Which of the following methods is a method of wrapper Integer for obtaining hash code for the
invoking object?
A. Integer hashcode()
B. int hashcode()
C. int hashCode()
D. int hash()
View Answer
Ans : C
Explanation: int hashCode() methods is a method of wrapper Integer for obtaining hash code for the
invoking object
15. Which of the following method of Process class can terminate a process??
A. void terminate()
B. void destroy()
C. void exit()
D. void kill()
View Answer
Ans : B
Explanation: Kills the subprocess. The subprocess represented by this Process object is forcibly
terminated.
Threads
A. run();
B. init();
C. start();
D. resume();
View Answer
Ans : C
Explanation: The start() method causes this thread to begin execution; the Java Virtual Machine calls
the run method of this thread.
Ans : C
Explanation: notify() - wakes up a single thread that is waiting on this object's monitor.
A. notify()
B. notifyall()
C. wait()
D. exits synchronized code
View Answer
Ans : C
Explanation: . wait() causes the current thread to wait until another thread invokes the notify()
method or the notifyAll() method for this object.
4. Which function of pre defined class Thread is used to check weather current thread being checked
is still running?
A. isAlive()
B. Alive()
C. isRunning()
D. Join()
View Answer
Ans : A
Explanation: isAlive() function is defined in class Thread, it is used for implementing multithreading
and to check whether the thread called upon is still running or not.
5. Which method must be defined by a class implementing the java.lang.Runnable interface?
View Answer
Ans : A
Explanation: In an interface all methods are abstract by default therefore they must be overridden
by the implementing class. The Runnable interface only contains 1 method, the void run() method
therefore it must be implemented.
6. Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000); After calling this method, when will the thread A become a candidate to get another
turn at the CPU?
View Answer
Ans : A
Explanation: Either of the two events (notification or wait time expiration) will make the thread
become a candidate for running again.
A. main();
B. stop();
C. start();
D. run();
View Answer
Ans : D
Explanation: The run() method to a thread is like the main() method to an application. Starting the
thread causes the object's run method to be called in that separately executing thread.
A. Object
B. Class
C. Runnable
D. Thread
View Answer
Ans : A
9. Which of these method of Thread class is used to find out the priority given to a thread?
A. ThreadPriority()
B. get()
C. getPriority()
D. getThreadPriority()
View Answer
Ans : C
Explanation: getPriority() method of Thread class is used to find out the priority given to a thread.
10. Which of these method of Thread class is used to Suspend a thread for a period of time?
A. stop()
B. sleep()
C. terminate()
D. suspend()
View Answer
Ans : B
Explanation: sleep() method of Thread class is used to Suspend a thread for a period of time.
int a=15;
int b=25;
system.out.println(a);
else
system.out.println(b);
A. Error
B. 15
C. 25
D. No output
View Answer
Ans : B
Explanation: No explanation
int x, y;
x=15; y=20;
if (x>15)
if(y>15)
system.ptintln("y is "+y);
else
system.out.ptintln("x is "+x);
A. Error
B. y is 20
C. x is 15
D. No output
View Answer
Ans : C
Explanation: No explanation.
switch(x)
{
default:
System.out.println("Hello");
A. short
B. char
C. long
D. float
View Answer
Ans : A
Explanation: Switch statements are based on integer expressions and since both bytes and chars can
implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long
are wrapper classes and reference types can not be used as variables.
int odd = 1;
if(odd) /* Line 4 */
System.out.println("odd");
else
System.out.println("even");
}
A. Compilation fails.
B. "odd" will always be output.
C. "even" will always be output.
D. "odd" will be output for odd values of x, and "even" for even values.
View Answer
Ans : A
Explanation: The compiler will complain because of incompatible types (line 4), the if expects a
boolean but it gets an integer.
int x= 0;
while ( 1 ) /* Line 6 */
View Answer
Ans : D
Explanation: Using the integer 1 in the while statement, or any other looping or conditional
construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.
A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name).
Line 8 is also valid because an equation may be placed in a String operation as shown.
View Answer
Ans : A
Explanation: The first option is not a valid declaration as i/9 is not declared correctly.
A. break
B. continue
C. exit()
D. return
View Answer
Ans : C
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running
JVM.
A. innermost
B. outermost
C. break statement causes an exit from program
D. Depends on program
View Answer
Ans : A
Explanation: The break statement causes an exit from innermost loop or switch.
A. switch
B. if-else
C. if
D. do-while
View Answer
Ans : D
10. What is the valid data type for variable "a" to print "Hello World"?
switch(a)
System.out.println("Hello World");
View Answer
Ans : D
Explanation: The switch condition would only meet if variable "a" is of type byte or char.
class selection_statements
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : B
Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets
executed.
12. What is the output of this program?
class comma_operator
int sum = 0;
sum += i;
System.out.println(sum);
A. 5
B. 6
C. 14
D. compilation error
View Answer
Ans : B
Explanation: Using comma operator, we can include more than one statement in the initialization
and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value –
0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
A. j = -1
B. j = 0
C. j = 1
D. Compilation fails.
View Answer
Ans : D
Explanation: The case statement takes only a single argument. The case statement on line 4 is given
two arguments so the compiler complains.
14. In the following code snippet, which lines of code contain error?
int j=0;
j++;
A. Line 2
B. Line 3
C. Line 4
D. Line 5
View Answer
Ans : A
Explanation: No explanation.
int i = 1, j = 10;
do
{ if(i > j)
break;
j--;
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 4
D. i = 5 and j = 6
View Answer
Ans : D
Explanation: This loop is a do-while loop, which always executes the code block within the block at
least once, due to the testing condition being at the end of the loop, rather than at the beginning.
This particular loop is exited prematurely if i becomes greater than j.The order is, test i against j, if
bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-
incremented by one i is tested for being lower than 5.
Java Packages
A. Classes
B. Sub Packages
C. Interfaces
D. All of the above
View Answer
Ans : D
Explanation: Package in Java is a mechanism to encapsulate a group of classes, sub packages and
interfaces.
A. pkg
B. Pkg
C. package
D. Package
View Answer
Ans : C
A. TRUE
B. FALSE
C. can be true or false
D. can not say
View Answer
Ans : A
Explanation: Package names and directory structure are closely related is true statement.
4. An _______________ statement can be used to access the classes and interface of a different
package from the current package.
A. instanceOf
B. import
C. extends
D. implement
View Answer
Ans : B
Explanation: This is the keyword which can be used to access the interface of a different package
from the current package.
5. Which of the following packages is used to includes classes to create user interface like Button and
Checkbox?
A. java.lang
B. java.net
C. java.awt
D. java.io
View Answer
6. Which of the following packages is used to includes utility classes like Calendar, Collection, Date?
A. java.lang
B. java.net
C. java.awt
D. java.util
View Answer
Ans : D
7. 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?
A. Public
B. Protected
C. No Modifier
D. All of the mentioned
View Answer
Ans : D
Explanation: Either we can use public, protected or we can name the class without any specifier.
8. Which of the following is the correct way of importing an entire package "pkg"?
A. import pkg.
B. Import pkg.
C. import pkg.*
D. Import pkg.*
View Answer
Ans : C
View Answer
Ans : C
Explanation: Packages cannot be considered as data encapsulation is false statement about package
in java.
10. Packages that are inside another package are the _________
A. packages
B. nested packages
C. util subpackages
D. subpackages
View Answer
Ans : D
Explanation: Packages that are inside another package are the subpackages.
Inheritance
1. ____________ can be defined as the process where one class acquires the properties (methods
and fields) of another.
A. Overriding
B. Inheritance
C. Polymorphism
D. Abstraction
View Answer
Ans : B
Explanation: Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another
A. superclass
B. parent class
C. subclass
D. None of the above
View Answer
Ans : C
Explanation: The class which inherits the properties of other is known as subclass
View Answer
Ans : D
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, the class whose properties are inherited is known as superclass
A. inherit
B. poly
C. extends
D. super
View Answer
Ans : C
A. construct
B. this
C. class
D. extends
View Answer
Ans : B
A. public member
B. protected member
C. static member
D. private member
View Answer
Ans : D
A. class B + class A {}
B. class B inherits class A {}
C. class B extends A {}
D. class B extends class A {}
View Answer
Ans : C
A. IS-A
B. HAS-A
C. ARE-A
D. HAD-A
View Answer
Ans : A
10. A subclass inherits all the members (fields, methods, and nested classes) from its superclass
A. Yes
B. NO
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: YES, A subclass inherits all the members (fields, methods, and nested classes) from its
superclass.
A. abstract
B. util
C. extends
D. None of the above
View Answer
Ans : A
Explanation: A class which is declared with the abstract keyword is known as an abstract class in
Java.
A. TRUE
B. FALSE
C. Abstract class can have constructors but can not have static methods.
D. Abstract class can not have constructors but can have static methods.
View Answer
Ans : A
A. abstract A{}
B. abstract class A
C. abstract class A{}
D. abstract class A[]
View Answer
Ans : C
A. Thread
B. AbstractList
C. List
D. None of the Mentioned
View Answer
Ans : A
5. A method which is declared as abstract and does not have implementation is known as an
_____________?
A. Abstract Interface
B. Abstract Thread
C. Abstract List
D. abstract Method
View Answer
Ans : D
Explanation: A method which is declared as abstract and does not have implementation is known as
an abstract method.
View Answer
Ans : A
7. An abstract class can have a data member, abstract method, method body (non-abstract method),
constructor, and even main() method.
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : A
Explanation: Yes, An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor, and even main() method.
View Answer
Ans : C
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class does
not contain any definition of implementation it is not possible to create an abstract object.
9. What will be output for the folllowing code?
View Answer
Ans : A
Explanation: Yes, you are right!! As private method can't be overridden and abstract method should
be overridden in child classes, so this line will give compilation error.
10. What will be output for the folllowing code?
Bank(String bankName)
this.bankName = bankName;
return bankName;
office() {
super("Axis Bank");
System.out.println(bank.getBankName());
}
A. Compilation error will occur because ""abstract class cannot have constructor""
B. Compilation error will occur because ""abstract class must have an abstract method""
C. Compilation error will occur while invoking the super class constructor
D. Code will be compiled successfully
View Answer
Ans : D
A. main()
B. recursive()
C. System defined methods
D. Any method
View Answer
Ans : A
Explanation: Only main() method can be given parameters via using command line arguments.
2. Which of these data types is used to store command line arguments?
A. Array
B. Stack
C. String
D. Integer
View Answer
Ans : C
A. 1
B. 2
C. 3
D. Infinite
View Answer
Ans : D
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
class CommandLineExample{
A. java
B. CommandLineExample{
C. arg[0]
D. letsfindcourse
View Answer
Ans : D
6. What will be the output of the following Java program, Command line exceution is done as – “java
Output This is a command Line”?
class Output
System.out.print(""args[3]"");
A. java
B. this
C. is
D. command
View Answer
Ans : D
7. Which annotation is used to represent command line input and assigned to correct data type?
A. @Input
B. @Variable
C. @Command Line
D. @Parameter
View Answer
Ans : D
8. ___________ is a very small Java framework that makes it trivial to parse command line
parameters.
A. Command Line
B. Jcommander
C. Args
D. None of the above
View Answer
Ans : B
Explanation: JCommander is a very small Java framework that makes it trivial to parse command line
parameters.
9. Which tab is used to pass command line argument in eclipse?
A. Arguments
B. Variable
C. Both A and B
D. None of the above
View Answer
Ans : A
class abc
if(args.length>0)
System.out.println(args.length);
View Answer
Ans : D
Explanation: As no argument is passed to the code, the length of args is 0. So the code will not print.
Java.io
A. input
B. output
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: Java I/O (Input and Output) is used to process the input and produce the output.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: Java uses the concept of a stream to make I/O operation fast. The java.io package
contains all the classes required for input and output operations. We can perform file handling in
Java by Java I/O API.
3. In java, how many streams are created for us automatically?
A. 2
B. 3
C. 4
D. 5
View Answer
Ans : B
Explanation: In Java, 3 streams are created for us automatically. All these streams are attached with
the console.
1. System.out
2. System.in
3. System.err
View Answer
Ans : A
Explanation: public void write(int)throws IOException is used to write a byte to the current output
stream.
5. Which method is used to write an array of byte to the current output stream?
View Answer
Ans : B
Explanation: public void write(byte[])throws IOException is used to write an array of byte to the
current output stream.
A. File
B. StringReader
C. Writer
D. String
View Answer
Ans : D
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : B
A. IsFile()
B. Isfile()
C. isFile()
D. isfile()
View Answer
Ans : C
Explanation: isFile() returns true if called on a file and returns false when called on a directory.
import java.io.*;
class files
System.out.print(obj.getName());
A. java
B. system
C. java/system
D. /java/system
View Answer
Ans : B
import java.io.*;
class files
System.out.print(obj.canWrite());
A. true false
B. false true
C. true true
D. false false
View Answer
Ans : D
Applets
1. An applet is a Java class that extends the?
A. java.Applet class
B. java class
C. Applet class
D. java.applet.Applet class
View Answer
Ans : D
A. Javascript
B. Css
C. HTML
D. SQL
View Answer
Ans : C
A. JCM
B. JDM
C. JVM
D. Java class
View Answer
Ans : C
Explanation: A JVM is required to view an applet. The JVM can be either a plug-in of the Web
browser or a separate runtime environment.
4. Which method is automatically called after the browser calls the init method?
A. start
B. stop
C. destroy
D. paint
View Answer
Ans : A
Explanation: start : This method is automatically called after the browser calls the init method. It is
also called whenever the user returns to the page containing the applet after having gone off to
other pages.
5. Which method is only called when the browser shuts down normally?
A. start
B. stop
C. destroy
D. paint
View Answer
Ans : C
Explanation: destroy : This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources behind after a
user leaves the page that contains the applet.
6. Which of these operators can be used to get run time information about an object?
A. getInfo
B. Info
C. instanceof
D. getinfoof
View Answer
Ans : C
Explanation: instanceof can be used to get run time information about an object
View Answer
Ans : B
Explanation: public void loop() : Causes the audio clip to replay continually
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
A. display()
B. Print()
C. drawString()
D. transient()
View Answer
Ans : C
import java.awt.*;
import java.applet.*;
A. A Simple Applet
B. A Simple Applet 20 20
C. Compilation Error
D. Runtime Error
View Answer
Ans : A
49. (i) Java is cohesive and logically not consistent Language. (ii) Java gives the programmer’s, full
control.
1. (i)False (ii)True
2. (i)True (ii)False
3. (i)False (ii)False
4. (i)True (ii)True
50. (i) Java is not a language with training wheels. It is a language for professional programmers
(ii) Java as simply the “Internet version of C++.”
1. (i)False (ii)True
2. (i)True (ii)False
3. (i)False (ii)False
4. Java language was initially called “Oak” but was renamed “Java” in 1995.
1. Correct
2. Incorrect
5. Java can be used to create two types of programs: applications and ___
1. applets
2. servlets
3. both 1 and 2
4. none of the above
6. The output of a Java compiler is not executable code. Rather, it is byte code.
1. Valid statement
2. Invalid statement
7. Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time
system, which is called the ___
1. JVM
2. IDE
3. working environment
4. all of the above
10. Java was not designed to be source-code compatible with any other language.
1. true
2. false
11. Java was designed to meet the real-world requirement of creating interactive, networked
Programs
1. correct
2. incorrect
12. Java enables the creation of cross-platform programs by compiling into an intermediate
representation called ___
1. code
2. intermediate code
3. Java byte code
4. none of the above
13. Remote Method Invocation (RMI). This feature brings an unparalleled level of abstraction to___
programming
1. socket
2. client/server
3. c++
4. C
14. ___programs carry with them substantial amounts of run-time type information that is used to
verify and resolve accesses to objects at run time.
1. C++
2. C
3. Java
4. all of the above
18. ___ is a set of user interface components that is implemented entirely in Java
1. intermediate code
2. Byte code
3. Swing
4. Servelets
19. Collections are groups of objects. Java 2 provides several types of collections, such as ___, for
your use. Collections offer a new way to solve several common programming problems.
1. linked lists
2. Dynamic arrays
3. Hash table
4. all of the above
23. When Java source code is compiled, each individual class is put into its own output file named
after the class and using the .class extension.
1. True
2. False
25. Java allows two or more statements to be grouped into blocks of code, also called ___
1.code
2. blocks.
3. code blocks
4. none of the above
26. Java is a free-form language. This means that you do not need to follow any special indentation
rules.
1. valid
2. Invalid statement
28. Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double,
and boolean.
1. True
2. False
29. Java allows variables to be initialized ___
1. statically
2. dynamically
3. both of the above
4. none of the above
31. The ability to create robust programs was given a ___ priority in the design of Java.
1. low
2. high
3. medium
4. all of the above
32. Java Virtual Machine is an attempt to the goal that was “write once; run anywhere, any time,
forever.”
1. True
2. False
Qs33. Java is designed for the distributed environment of the Internet because it handles ___
protocols
1. TCP
2. IP
3. HTTP
4. both 1 and 2
34. ___ is the mechanism that binds together code and the data it manipulates and keeps both safes
from outside interference and misuse.
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Multithreaded
35. A ___ defines the structure and behaviour (data and code) that will be shared by a set of objects.
1. Class
2. Objects
3. group of objects
4. Inheritance
39. ___ allows you to migrate your implementations over time without breaking the code that
depends on the public interface of your classes.
1. Encapsulation
2. polymorphism
3. Portability
4. none of the above
41. Java was designed to meet the real-world requirement of creating ___ programs. To accomplish
this, Java supports ___ threaded programming, which allows you to write programs that do many
things simultaneously.
1. interactive, multi
2. networked, uni
3. interactive and networked, multi
4. web-enabled, dual
42. Java enables the creation of cross-platform programs by compiling into an intermediate
representation called Java___. This code can be interpreted on any system that provides a ___
1. bytecode, Java Virtual Machine.
2. code, IDE
3. intermediate code, JNI
4. bytecode, JNI
43. Java programs carry with them substantial amounts of ___ time type information that is used to
verify and resolve accesses to objects at ___ time
1. run, run
1. compile, run
2. run, compile
3. compile, compile
44. The concept of ___is often expressed by the phrase “one interface, multiple methods.” This
means that it is possible to design a generic interface to a group of related activities. This helps
reduce ___–by allowing the same interface to be used to specify a general class of action
1. polymorphism, simplicity
2. Inheritance, complexity
3. polymorphism, complexity
4. Multithreading, complexity
45. From ___, Java derives its syntax object-oriented features were influenced by___
1. C, C++
2. C#, C++
3. java script, C#
4. VB.net, C
46___and others began work on a portable, platform-independent language that could be used to
produce code that would run on a variety of CPUs under differing environments. This effort
ultimately led to the creation of ___
1. Gosling, Java.
2. Dennis, C++
3. Gosling, java
4. Dennis, C
47. OOP is a programming methodology that helps organize complex programs through the use of
inheritance,___ and ___
1. encapsulation, polymorphism
2. multithreading, data abstraction
3. Data hiding, encapsulation
4. polymorphism, data hiding
48. Java is also a ___ language. Java derives much of its character from ___and C++.
1. Programmer’s, C
2. system’s, C++
3. operating system, Visual Basic
4. programmer’s, Java
49. (i) Java is cohesive and logically not consistent Language. (ii) Java gives the programmer’s, full
control.
1. (i)False (ii)True
2. (i)True (ii)False
3. (i)False (ii)False
4. (i)True (ii)True
50. (i) Java is not a language with training wheels. It is a language for professional programmers
(ii) Java as simply the “Internet version of C++.”
1. (i)False (ii)True
2. (i)True (ii)False
3. (i)False (ii)False
4. (i)True (ii)True
1. The earlier name of Java was ____ ____.
Ans. Oak
2. The members of the Green Project were ____, ____ and ____.
Ans. Patrick Naughton, Mike Sheridan, James Gosling
10. ____ is used to create and manage public keys, private keys and security certificates.
Ans. Keytool
11. All information which is managed by the key tool is stored in a database called____.
Ans. Keystore
12. Sun Microsystems includes a default key store that uses a new file format called____.
Ans. JKS (Java Key Store)
13. Java archival tool used for packaging all java programs and resource files into a single archive file
is____.
Ans. Jar
14. ____ was the first programming language that could be used to send interactive programs over
the World Wide Web.
Ans. Java.
15. A ____ is an encrypted file or files that accompany a program, indicating exactly from whom the
file came.
Ans. Digital signature
16. The document that represents this digital signature is called a ____.
Ans. Certificate Ans
17. An applet provider must verify its identity using a group called a ____.
Ans. Certificate Authority.
18. A compiler converts the Java program into an intermediate language representation called ____.
Ans. bytecode
20. The third type of comment is used by a tool called ____ for automatic generation of
documentation.
Ans. Javadoc
26. A ____ access specifier allows the method to be executed from another class.
Ans. public
34. Both the String and StringBuffer classes are defined in ____ package.
Ans. java.lang
35. ____ represents a number of variables which occupy contiguous spaces in the memory.
Ans. array
38. To extract a single character from a String, you can refer directly to an individual character via
the ____ method.
Ans. charAt( )
39. To extract more than one character at a time, you can use the ____ method.
Ans. getChars( )
40. To extract a single character from a String, you can use ____ method.
Ans. charAt()
41. To extract more than one character at a time, you can use ____ method.
Ans. getChars()
43. While searching string ____ searches for the first occurrence of a character or substring.
Ans. indexOf( )
44. While searching string ____ Searches for the last occurrence of a character or substring.
Ans. lastIndexOf( )
45. ____ method searches for the first occurrence of a character or substring.
Ans. indexOf()
46. ____ method searches for the last occurrence of a character or substring.
Ans. lastIndexOf()
48. To set the length of the buffer within a StringBuffer object, use ____ method.
Ans. setLength()
49. The ____ method concatenates the string representation of any other type of data to the end of
the invoking StringBuffer object.
Ans. append()
50. You can reverse the characters within a StringBuffer object using ____ method.
Ans. reverse()
51. ____ is one of the features of object-oriented programming that allows the creation of
hierarchical classifications.
Ans. inheritance
55. ____, ____ and ____ are the three types of access specifiers in Java.
Ans. public, private, protected
56. ____ is the mechanism in Java through which the class namespace is partitioned into more
manageable chunks.
Ans. package
59. The specific location that the Java compiler will consider as the root of any package hierarchy is
controlled by ____.
Ans. CLASSPATH
62. Variables declared inside the interface declarations are implicit ____ and ____.
Ans. final, static
64. ____ and ____ classes are derived from the Throwable class.
Ans. Error and Exception
67. ____ exception will be thrown if you try to access the array element beyond its index value.
Ans. ArrayIndexOutOfBounds.
70. The class at the top of the exception classes hierarchy is ____.
Ans. Throwable.
1. ___ and ___ classes are derived from the Throwable class.
Ans. Error and Exception.
2. Exception classes are available in the ___ package.
Ans. java.lang
4. ___ exception will be thrown if you try to access the array element beyond its index value.
Ans. ArrayIndexOutOfBounds.
5. Java programs written to run on World Wide Web (WWW) are called ___.
Ans. applets.
6. Applets can be tested using the ___ tool included in the Java Development Kit.
Ans. applet viewer
7. When the user interacts with the application by pressing a key or clicking a mouse button, an ___
is generated.
Ans. event
9. When the user clicks on the button, the ___ event is generated.
Ans. ActiionEvent
11. An ___ event is generated when an item from a list, a choice, or a check box is selected.
Ans. ItemEvent.
12. The ___ method draws the graphics of the applet in the drawing area.
Ans. paint()
15. The Applet tag is written in the ___ tag of an HTML document.
Ans. body
22. A ___ is a collection of related information and a ___ is the software that provides you with a
mechanism to manipulate data in the database.
Ans. Database, Database Management System (DBMS).
23. MS-Access stores the data in ___ file format whereas MS-SQL Server stores the data in a ___ file
format.
Ans. .MDB, .DAT
25. A ___ is a service that runs on a server and helps the objects on other hosts to remotely access
its registered objects.
Ans. Remote Registry Server.
26. Objects that are exported for remote access must implement the interface called ___.
Ans. remote interface.
28. A ___ is a small pluggable extension to a server that enhances the server’s functionality.
Ans. Servlet.
31. Servlet Life Cycle contains ___, ___ and ___ methods.
Ans. init(), service(), destroy().
37. The right-hand window of the bean box contains ___showing the properties for the currently
selected bean.
Ans. PropertySheet.
39. All the methods that are to be invoked remotely must throw the exception called ___.
Ans. RemoteException.
40. RMI uses the ___ protocol for transporting the information.
Ans. TCP
41. ___ is an abstract class that is used to create a format for opening a stream connection to a
specific URL.
Ans. URLStreamHandler
42. The ___ method is used to report that a thread has died.
Ans. threadDeathEvent()
43. ___ package contains all the network related classes and methods of Java.
Ans. java.net
44. ___ method opens a connection to an URL and enables to extract data from that connection.
Ans. getInputStream()
45. ___ class is used to read a stream of data that is generated by a network connection and produce
an object.
Ans. ContentHandler()
46. ___ is an abstract class that is used to create and control a connection to a platform- and
firewall-specific location.
Ans. URLConnection
47. The heart of the Java programming language is contained in a set of packages called ___, which is
a part of the Java API.
Ans. java.lang,
48. The JDK also includes a package called ___, which is designed to make the application-debugging
process easier.
Ans. sun.tools.debug,
49. ___ and ___ are two Java interpreters.
Ans. java, java_g
50. The various stacks in existence as a program runs are called the ___.
Ans. stack frame.
53. ___ was the first programming language that could be used to send interactive programs over
the World Wide Web.
Ans. Java
54. A ___ is an encrypted file or files that accompany a program, indicating exactly from whom the
file came.
Ans. Digital signature
55. The document that represents this digital signature is called a ___.
Ans. Certificate.
56. An applet provider must verify its identity using a group called a ___.
Ans. Certificate Authority
57. ___ is used to create and manage public keys, private keys and security certificates.
Ans. Keytool.
58. All information that is managed by a key tool is stored in a database called ___.
Ans. Keystore
59. Sun Microsystems includes a default key store that uses a new file format called ___.
Ans. JKS (Java Key Store)
60. The Java archival tool ___ is used to pack into a single archive file a Java program and all resource
files that it requires.
Ans. Jar
**********************************************************************************
********************************************************************
Rajput
2. Employee emp = ___ Employee (); Pick a suitable word from the list so
that an object of the class Employee is created.
A) object
B) class
C) run
D) new
10. Which of the following operators are used to compare two values and
give the results:
A) Increment and Decrement
B) Logical
C) Comparison
D) Arithmetic
11. What is the result of the expression: 10+5*8-15/5
A) 3
B) 47
C) 7
D) 21
14. Which loop always executes its body at least once, even though the
condition is not true:
A) for
B) do-while
C) while
D) continue
16. Which of the following method can be used to set the size of the
buffer:
A) ensureCapacity( )
B) length( )
C) capacity( )
D) setLength( )
17. Which of the following method returns the reversed object on which
it is called:
A) insert( )
B) replace( )
C) delete( )
D) reverse( )
20. Both the String and StringBuffer classes are defined in ____ package:
A) java.awt
B) java.io
C) java.lang
D) java.util
22. Human Being and Elephant fall under which of the following
relationship:
A) Kind-Of
B) Is-A
C) Part-Of
D) Has-A
24. A class member that has been declared as private will be ___ to its
class.
A) Friendly
B) Public
C) Protected
D) Private
27. ___ is used as a base class to derive specific classes of the same kind.
A) private
B) friend class
C) abstract class
D) superclass
36. Which of the following class lays the foundation for the output class
hierarchy:
A) Reader
B) InputStream
C) OutputStream
D) None of the above
38. The streams that are read from or written to a specific place like a
disk file or memory:
A) Markable streams
B) ByteArray streams
C) Mode streams
D) Filter streams
40. Which of the methods is called immediately after the init() is called:
A) destroy( )
B) start( )
C) paint( )
D) stop( )
41. Context can be retrieved using which of the following method:
A) getNum( )
B) getGraphics( )
C) getAppletContext( )
D) getText( )
43. The argument temporaryFlag is set to true if the focus event is:
A) temporary
B) gained
C) lost
D) retrieved
45. Which of the following is at the top of the event class hierarchy:
A) java.awt.AWTEvent
B) java.awt.EventObject
C) java.util.EventObject
D) None of the above
48. The method that returns the event ID that represents the nature of
the event:
A) getId( )
B) getNature( )
C) getName( )
D) getValue( )
49. Which of the following Swing Components hierarchically extends
AWT applet:
A) JTree
B) JToolTip
C) JApplet
D) JPanel
A) a
A) b
A) a
A) b
(a) static (b) const (c) final (d) abstract (e) none of the above.
A) c
A) c
A) c
A) c
10) Which of the following is true?
(a) A finally block is executed before the catch block but after the try block.
(b) A finally block is executed, only after the catch block is executed.
(c) A finally block is executed whether an exception is thrown or not.
(d) A finally block is executed, only if an exception occurs.
(e) None of the above.
A) c
A) d
After the above piece of code is executed, what are the colors of r1 and
r2 (in this order)?
(a) Color.blue
Color.red
(b) Color.blue
Color.blue
(c) Color.red
Color.red
(d) Color.red
Color.blue
(e) None of the above.
A) c
13) What is the type and value of the following expression? (Notice the integer
division)
-4 + 1/2 + 2*-3 + 5.0
A) d
A) c
A) d
16) Which of the following variable declaration would NOT compile in a java
program?
(a) int var; (b) int VAR; (c) int var1; (d) int var_1; (e) int 1_var;.
A) e
The method setValue assigns the value of i to the instance field value. What could
you write for the implementation of setValue?
(a) value = i; (b) this.value = i; (c) value == i;
(d) Both (A) and (B) and above (e) (A), (B) and (C) above.
A) d
A) d
19) A constructor
(a) Must have the same name as the class it is declared within.
(b) Is used to create objects.
(c) May be declared private
(d) Both (A) and (B) above
(e) (a), (b) and (c) above.
A) e
20) Consider,
A) a
A) a
Java Multiple Choice Questions 22) What is garbage collection in the context
of Java?
(a) The operating system periodically deletes all the java files available on the
system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is
automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that
doesn’t make sense.
(e) Janitors working for Sun Micro Systems are required to throw away any Microsoft
documentation found in the employees’ offices.
A) c
Java Multiple Choice Questions 23) You read the following statement in a Java
program that compiles and executes.
submarine.dive(depth);
A) b
Java Multiple Choice Questions 24) The java run time system automatically
calls this method while garbage collection.
A) b
Java Multiple Choice Questions 25) The correct order of the declarations in a
Java program is,
A) a
A) d
Java Multiple Choice Questions 27) A protected member can be accessed in,
(a) a subclass of the same package (b) a non-subclass of the same package
(c) a non-subclass of different package (d) a subclass of different package
(e) the same class.
A) c
Java Multiple Choice Questions 28) What is the output of the following code:
class eq
{
public static void main(String args[])
{
String s1 = “Hello”;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}
A) b
Java Multiple Choice Questions 29) All exception types are subclasses of the
built-in class
A) d
Java Multiple Choice Questions 30) When an overridden method is called from
within a subclass, it will always refer to the version of that method defined by
the
A) b
**********************************************************************************
********************************************************************
Omkar
1.-concepts-oops/ 1-10
2.-floating-data-types/11-20
3. character-boolean-data-types/ 21-30
4.data-type-enums/ 31-40
5. https://www.sanfoundry.com/java-mcqs-arrays-2/ 41-50
7. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
View Answer
Answer: d
Explanation: It is a relationship where all objects have their own lifecycle and
there is no owner. This occurs where many to many relationships are
available, instead of one to one or one to many.
8. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
View Answer
Answer: b
Explanation: Composition occurs when child object gets killed if parent object
gets killed. Aggregation is also known as strong Aggregation.
9. What is it called where object has its own lifecycle and child object cannot
belong to another parent object?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
View Answer
Answer: a
Explanation: Aggregation occurs when objects have their own life cycle and
child object can associate with only one parent object.
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct
View Answer
Answer: d
Explanation: Statements (1), (2), (3), and (4) are correct. (1) is correct
because when a floating-point number (a double in this case) is cast to an int,
it simply loses the digits after the decimal. (2) and (4) are correct because a
long can be cast into a byte. If the long is over 127, it loses its most
significant (leftmost) bits. (3) actually works, even though a cast is not
necessary, because a long can store a byte.
15. Which of these literals can be contained in float data type variable?
a) -1.7e+308
b) -3.4e+038
c) +1.7e+308
d) -3.4e+050
View Answer
Answer: b
Explanation: Range of float data type is -(3.4e38) To +(3.4e38)
16. Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long
View Answer
Answer: c
1. class average {
2. public static void main(String args[])
3. {
4. double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
5. double result;
6. result = 0;
7. for (int i = 0; i < 6; ++i)
8. result = result + num[i];
9. System.out.print(result/6);
10.
11. }
12. }
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
View Answer
Answer: c
$ javac average.java
$ java average
16.46666666666667
1. class output {
2. public static void main(String args[])
3. {
4. double a, b,c;
5. a = 3.0/0;
6. b = 0/4.0;
7. c=0/0.0;
8.
9. System.out.println(a);
10. System.out.println(b);
11. System.out.println(c);
12. }
13. }
a) Infinity
b) 0.0
c) NaN
d) all of the mentioned
View Answer
Answer: d
Explanation: For floating point literals, we have constant value to represent
(10/0.0) infinity either positive or negative and also have NaN (not a number
for undefined like 0/0.0), but for the integral type, we don’t have any
constant that’s why we get an arithmetic exception.
1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }
a) 25
b) 24
c) 32
d) 33
View Answer
Answer: c
Explanation: Operator ++ has more preference than *, thus g becomes 4 and
when multiplied by 8 gives 32.
output:
$ javac increment.java
$ java increment
32
1. class area {
2. public static void main(String args[])
3. {
4. double r, pi, a;
5. r = 9.8;
6. pi = 3.14;
7. a = pi * r * r;
8. System.out.println(a);
9. }
10. }
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
View Answer
Answer: a
Explanation: None.
output:
$ javac area.java
$ java area
301.5656
22. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned
View Answer
Answer: c
24. Which of these occupy first 0 to 127 in Unicode character set used for
characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1
View Answer
Answer: d
Explanation: First 0 to 127 character set in Unicode are same as those of ISO-
LATIN-1 and ASCII.
1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i] + "" );
8. i++;
9. }
10. }
11. }
a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
output:
$ javac array_output.java
$ java array_output
i i i i i
1. class mainclass {
2. public static void main(String args[])
3. {
4. char a = 'A';
5. a++;
6. System.out.print((int)a);
7. }
8. }
a) 66
b) 67
c) 65
d) 64
View Answer
Answer: a
Explanation: ASCII value of ‘A’ is 65, on using ++ operator character value
increments by one.
output:
$ javac mainclass.java
$ java mainclass
66
1. class mainclass {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. if (var1)
7. System.out.println(var1);
8. else
9. System.out.println(var2);
10. }
11. }
a) 0
b) 1
c) true
d) false
View Answer
Answer: c
Explanation: None.
output:
$ javac mainclass.java
$ java mainclass
true
1. class booloperators {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. System.out.println((var1 & var2));
7. }
8. }
a) 0
b) 1
c) true
d) false
View Answer
Answer: d
Explanation: boolean ‘&’ operator always returns true or false. var1 is defined
true and var2 is defined false hence their ‘&’ operator result is false.
output:
$ javac booloperators.java
$ java booloperators
false
1. class asciicodes {
2. public static void main(String args[])
3. {
4. char var1 = 'A';
5. char var2 = 'a';
6. System.out.println((int)var1 + " " + (int)var2);
7. }
8. }
a) 162
b) 65 97
c) 67 95
d) 66 98
View Answer
Answer: b
Explanation: ASCII code for ‘A’ is 65 and for ‘a’ is 97.
output:
$ javac asciicodes.java
$ java asciicodes
65 97
1. enum Season
2. {
3. WINTER, SPRING, SUMMER, FALL
4. };
5. System.out.println(Season.WINTER.ordinal());
a) 0
b) 1
c) 2
d) 3
View Answer
Answer: a
Explanation: ordinal() method provides number to the variables defined in
Enum
34. If we try to add Enum constants to a TreeSet, what sorting order will it
use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums
View Answer
Answer: a
Explanation: Tree Set will sort the values in the order in which Enum
constants are declared.
35. What will be the output of the following Java code snippet?
1. class A
2. {
3.
4. }
5.
6. enum Enums extends A
7. {
8. ABC, BCD, CDE, DEF;
9. }
a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception
View Answer
Answer: b
Explanation: Enum types cannot extend class.
36. What will be the output of the following Java code snippet?
1. enum Levels
2. {
3. private TOP,
4.
5. public MEDIUM,
6.
7. protected BOTTOM;
8. }
a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error
View Answer
Answer: d
Explanation: Enum cannot have any modifiers. They are public, static and
final by default.
37. What will be the output of the following Java code snippet?
1. enum Enums
2. {
3. A, B, C;
4.
5. private Enums()
6. {
7. System.out.println(10);
8. }
9. }
10.
11. public class MainClass
12. {
13. public static void main(String[] args)
14. {
15. Enum en = Enums.B;
16. }
17. }
a)
10
10
10
b) Compilation Error
c)
10
10
d) Runtime Exception
View Answer
Answer: a
Explanation: The constructor of Enums is called which prints 10.
a) 0
b) value stored in arr[0]
c) 00000
d) Class name@ hashcode in hexadecimal form
View Answer
Answer: d
Explanation: If we trying to print any reference variable internally, toString()
will be called which is implemented to return the String in following form:
classname@hashcode in hexadecimal form
1. class array_output
2. {
3. public static void main(String args[])
4. {
5. int array_variable [] = new int[10];
6. for (int i = 0; i < 10; ++i)
7. {
8. array_variable[i] = i;
9. System.out.print(array_variable[i] + " ");
10. i++;
11. }
12. }
13. }
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
View Answer
Answer: a
Explanation: When an array is declared using new operator then all of its
elements are initialized to 0 automatically. for loop body is executed 5 times
as whenever controls comes in the loop i value is incremented twice, first by
i++ in body of loop then by ++i in increment condition of for loop.
output:
$ javac array_output.java
$ java array_output
0 2 4 6 8
47. What will be the output of the following Java code?
1. class multidimention_array
2. {
3. public static void main(String args[])
4. {
5. int arr[][] = new int[3][];
6. arr[0] = new int[1];
7. arr[1] = new int[2];
8. arr[2] = new int[3];
9. int sum = 0;
10. for (int i = 0; i < 3; ++i)
11. for (int j = 0; j < i + 1; ++j)
12. arr[i][j] = j + 1;
13. for (int i = 0; i < 3; ++i)
14. for (int j = 0; j < i + 1; ++j)
15. sum + = arr[i][j];
16. System.out.print(sum);
17. }
18. }
a) 11
b) 10
c) 13
d) 14
View Answer
Answer: b
Explanation: arr[][] is a 2D array, array has been allotted memory in parts.
1st row contains 1 element, 2nd row contains 2 elements and 3rd row
contains 3 elements. each element of array is given i + j value in loop. sum
contains addition of all the elements of the array.
output:
$ javac multidimention_array.java
$ java multidimention_array
10
1. class evaluate
2. {
3. public static void main(String args[])
4. {
5. int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
6. int n = 6;
7. n = arr[arr[n] / 2];
8. System.out.println(arr[n] / 2);
9. }
10. }
a) 3
b) 0
c) 6
d) 1
View Answer
Answer: d
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n
is given value 3 printing arr[3]/2 i:e 3/2 = 1 because of int Value, by int
values there is no rest. If this values would be float the result would be 1.5.
output:
$ javac evaluate.java
$ java evaluate
1. class array_output
2. {
3. public static void main(String args[])
4. {
5. char array_variable [] = new char[10];
6. for (int i = 0; i < 10; ++i)
7. {
8. array_variable[i] = 'i';
9. System.out.print(array_variable[i] + "");
10. }
11. }
12. }
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
View Answer
Answer: d
Explanation: None.
output:
$ javac array_output.java
$ java array_output
i i i i i i i i i i
1. class array_output
2. {
3. public static void main(String args[])
4. {
5. int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8,
9}};
6. int sum = 0;
7. for (int i = 0; i < 3; ++i)
8. for (int j = 0; j < 3 ; ++j)
9. sum = sum + array_variable[i][j];
10. System.out.print(sum / 5);
11. }
12. }
a) 8
b) 9
c) 10
d) 11
View Answer
Answer: b
Explanation: None.
output:
$ javac array_output.java
$ java array_output
9
https://www.sanfoundry.com/tricky-java-questions-answers/
51. What is the type of variable ‘b’ and ‘d’ in the following Java snippet?
int a[], b;
int []c, d;
int arr[];
arr = new int[5];
a) 0
b) value stored in arr[0].
c) 00000
d) Garbage value
View Answer
Answer: d
Explanation: arr is an array variable, it is pointing to array of integers.
Printing arr will print garbage value. It is not same as printing arr[0].
54. What will be the output of the following Java code snippet?
a) ArrayIndexOutOfBoundsException
b) ArrayStoreException
c) Compilation Error
d) Code runs successfully
View Answer
Answer: b
Explanation: ArrayIndexOutOfBoundsException comes when code tries to
access an invalid index for a given array. ArrayStoreException comes when
you have stored an element of type other than the type of array.
61. What is the stored in the object obj in following lines of Java code?
box obj;
1. class main_class
2. {
3. public static void main(String args[])
4. {
5. int x = 9;
6. if (x == 9)
7. {
8. int x = 8;
9. System.out.println(x);
10. }
11. }
12. }
a) 9
b) 8
c) Compilation error
d) Runtime error
View Answer
Answer: c
Explanation: Two variables with the same name can’t be created in a class.
output:
$ javac main_class.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable x
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. }
7. class mainclass
8. {
9. public static void main(String args[])
10. {
11. box obj = new box();
12. obj.width = 10;
13. obj.height = 2;
14. obj.length = 10;
15. int y = obj.width * obj.height * obj.length;
16. System.out.print(y);
17. }
18. }
a) 12
b) 200
c) 400
d) 100
View Answer
Answer: b
Explanation: None.
output:
$ javac mainclass.java
$ java mainclass
200
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. }
7. class mainclass
8. {
9. public static void main(String args[])
10. {
11. box obj1 = new box();
12. box obj2 = new box();
13. obj1.height = 1;
14. obj1.length = 2;
15. obj1.width = 1;
16. obj2 = obj1;
17. System.out.println(obj2.height);
18. }
19. }
a) 1
b) 2
c) Runtime error
d) Garbage value
View Answer
Answer: a
Explanation: When we assign an object to another object of same type, all
the elements of right side object gets copied to object on left side of equal to,
=, operator.
output:
$ javac mainclass.java
$ java mainclass
1
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. }
7. class mainclass
8. {
9. public static void main(String args[])
10. {
11. box obj = new box();
12. System.out.println(obj);
13. }
14. }
a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
View Answer
Answer: d
Explanation: When we print object internally toString() will be called to return
string into this format classname@hashcode in hexadecimal form.
output:
$ javac mainclass.java
$ java mainclass
box@130671e
https://www.sanfoundry.com/java-mcqs-introduction-methods/
71. What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double
View Answer
Answer: c
Explanation: Return type of a method must be made void if it is not returning
any value.
72. What is the process of defining more than one method in a class
differentiated by method signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
View Answer
Answer: b
Explanation: Function overloading is a process of defining more than one
method in a class with same name differentiated by function signature i:e
return type or parameters type and number. Example – int volume(int length,
int width) & int volume(int length , int width , int height) can be used to
calculate volume.
73. Which of the following is a method having same name as that of it’s
class?
a) finalize
b) delete
c) class
d) constructor
View Answer
Answer: d
Explanation: A constructor is a method that initializes an object immediately
upon creation. It has the same name as that of class in which it resides.
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. int volume;
7. void volume(int height, int length, int width)
8. {
9. volume = width*height*length;
10. }
11. }
12. class Prameterized_method
13. {
14. public static void main(String args[])
15. {
16. box obj = new box();
17. obj.height = 1;
18. obj.length = 5;
19. obj.width = 5;
20. obj.volume(3,2,1);
21. System.out.println(obj.volume);
22. }
23. }
a) 0
b) 1
c) 6
d) 25
View Answer
Answer: c
Explanation: None.
output:
$ Prameterized_method.java
$ Prameterized_method
6
1. class equality
2. {
3. int x;
4. int y;
5. boolean isequal()
6. {
7. return(x == y);
8. }
9. }
10. class Output
11. {
12. public static void main(String args[])
13. {
14. equality obj = new equality();
15. obj.x = 5;
16. obj.y = 5;
17. System.out.println(obj.isequal());
18. }
19. }
a) false
b) true
c) 0
d) 1
View Answer
Answer: b
Explanation: None.
output:
$ javac Output.java
$ java Output
true
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. int volume;
7. void volume()
8. {
9. volume = width*height*length;
10. }
11. }
12. class Output
13. {
14. public static void main(String args[])
15. {
16. box obj = new box();
17. obj.height = 1;
18. obj.length = 5;
19. obj.width = 5;
20. obj.volume();
21. System.out.println(obj.volume);
22. }
23. }
a) 0
b) 1
c) 25
d) 26
View Answer
Answer: c
Explanation: None.
output:
$ javac Output.java
$ java Output
25
79. In the following Java code, which call to sum() method is appropriate?
1. class Output
2. {
3.
4. public static int sum(int ...x)
5. {
6. return;
7. }
8. static void main(String args[])
9. {
10. sum(10);
11. sum(10,20);
12. sum(10,20,30);
13. sum(10,20,30,40);
14. }
15. }
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
View Answer
Answer: d
Explanation: sum is a variable argument method and hence it can take any
number as an argument.
1. class area
2. {
3. int width;
4. int length;
5. int volume;
6. area()
7. {
8. width=5;
9. length=6;
10. }
11. void volume()
12. {
13. volume = width*length*height;
14. }
15. }
16. class cons_method
17. {
18. public static void main(String args[])
19. {
20. area obj = new area();
21. obj.volume();
22. System.out.println(obj.volume);
23. }
24. }
a) 0
b) 1
c) 30
d) error
View Answer
Answer: d
Explanation: Variable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height
https://www.sanfoundry.com/java-mcqs-constructors-garbage-collection/
82. Which keyword is used by the method to refer to the object that invoked
it?
a) import
b) catch
c) abstract
d) this
View Answer
Answer: d
Explanation: this keyword can be used inside any method to refer to the
current object. this is always a reference to the object on which the method
was invoked.
83. Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
View Answer
Answer: d
Explanation: A constructor is a method that initializes an object immediately
upon creation. It has the same name as that of class in which it resides.
84. Which operator is used by Java run time implementations to free the
memory of an object when it is no longer needed?
a) delete
b) free
c) new
d) none of the mentioned
View Answer
Answer: d
Explanation: Java handles deallocation of memory automatically, we do not
need to explicitly delete an element. Garbage collection only occurs during
execution of the program. When no references to the object exist, that object
is assumed to be no longer needed, and the memory occupied by the object
can be reclaimed.
85. Which function is used to perform some action when the object is to be
destroyed?
a) finalize()
b) delete()
c) main()
d) none of the mentioned
View Answer
Answer: a
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. int volume;
7. box()
8. {
9. width = 5;
10. height = 5;
11. length = 6;
12. }
13. void volume()
14. {
15. volume = width*height*length;
16. }
17. }
18. class constructor_output
19. {
20. public static void main(String args[])
21. {
22. box obj = new box();
23. obj.volume();
24. System.out.println(obj.volume);
25. }
26. }
a) 100
b) 150
c) 200
d) 250
View Answer
Answer: b
Explanation: None.
output:
$ constructor_output.java
$ constructor_output
150
1. class San
2. {
3. San()throws IOException
4. {
5.
6. }
7.
8. }
9. class Foundry extends San
10. {
11. Foundry()
12. {
13.
14. }
15. public static void main(String[]args)
16. {
17.
18. }
19. }
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. int volume;
7. void finalize()
8. {
9. volume = width*height*length;
10. System.out.println(volume);
11. }
12. protected void volume()
13. {
14. volume = width*height*length;
15. System.out.println(volume);
16. }
17. }
18. class Output
19. {
20. public static void main(String args[])
21. {
22. box obj = new box();
23. obj.width=5;
24. obj.height=5;
25. obj.length=6;
26. obj.volume();
27. }
28. }
a) 150
b) 200
c) Run time error
d) Compilation error
View Answer
Answer: a
Explanation: None.
output:
$ javac Output.java
$ java Output
150
1. class area
2. {
3. int width;
4. int length;
5. int area;
6. void area(int width, int length)
7. {
8. this.width = width;
9. this.length = length;
10. }
11.
12. }
13. class Output
14. {
15. public static void main(String args[])
16. {
17. area obj = new area();
18. obj.area(5 , 6);
19. System.out.println(obj.length + " " + obj.width);
20. }
21. }
a) 0 0
b) 5 6
c) 6 5
d) 5 5
View Answer
Answer: c
Explanation: this keyword can be used inside any method to refer to the
current object. this is always a reference to the object on which the method
was invoked.
output:
$ javac Output.java
$ java Output
6 5
https://www.sanfoundry.com/java-questions-answers-constructor/
92. What would be the behaviour if this() and super() used in a method?
a) Runtime error
b) Throws exception
c) compile time error
d) Runs successfully
View Answer
Answer: c
Explanation: this() and super() cannot be used in a method. This throws
compile time error.
93. What is false about constructor?
a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can have a return type
d) “this” and “super” can be used in a constructor
View Answer
Answer: c
Explanation: The constructor cannot have a return type. It should create and
return new objects. Hence it would give a compilation error.
94. What is true about Class.getInstance()?
a) Class.getInstance calls the constructor
b) Class.getInstance is same as new operator
c) Class.getInstance needs to have matching constructor
d) Class.getInstance creates object if class does not have any constructor
View Answer
Answer: d
Explanation: Class class provides list of methods for use like getInstance().
https://www.sanfoundry.com/java-questions-answers-heap-garbage-collection/
https://www.sanfoundry.com/java-mcqs-access-control/
https://www.sanfoundry.com/java-mcqs-overloading-methods-argument-passing/
https://www.sanfoundry.com/java-mcqs-access-control/
https://www.sanfoundry.com/java-interview-questions-answers/
https://www.sanfoundry.com/java-questions-bank/
https://www.sanfoundry.com/java-mcqs-string-class/
https://www.sanfoundry.com/java-questions-answers-entrance-exams/
https://www.sanfoundry.com/java-mcqs-command-line-arguments/
https://www.sanfoundry.com/java-questions-answers-freshers/
https://www.sanfoundry.com/java-mcqs-recursion/
https://www.sanfoundry.com/java-mcqs-method-overriding/
https://www.sanfoundry.com/java-mcqs-object-class/
https://www.sanfoundry.com/java-mcqs-inheritance-abstract-and-super/
https://www.sanfoundry.com/java-mcqs-inheritance/
https://www.sanfoundry.com/java-interview-questions-answers-freshers/
https://www.sanfoundry.com/java-mcqs-string-handling-basics/
https://www.sanfoundry.com/java-mcqs-character-extraction/
https://www.sanfoundry.com/java-mcqs-string-comparision/
https://www.sanfoundry.com/java-mcqs-seraching-modifying-string/
https://www.sanfoundry.com/java-mcqs-stringbuffer-class/
https://www.sanfoundry.com/java-mcqs-stringbuffer-methods/