Sunteți pe pagina 1din 91

3301 QUESTION BANK

What will be the output of the following code?


int j=50;
while(true)
{
if(j<10)
break;
j=j-10;
}
System.out.println(" j is "+j);
1. Error
2. j is 0
3. 0
4. No output
What will be the result of attempting to compile the following program?
public class MyClass {
long var;
public void MyClass(long p) { var = p; }
public static void main(String[] args) {
MyClass a,b;
a = new MyClass();
b = new MyClass(5);
}
}

1. A compilation ERROR will occur at (1), since constructors cannot specify a


return value
2. A compilation error will occur at (2), since the class does not have a default
constructor
3. A compilation error will occur at (3), since the class does not have a
constructor which takes one argument of type int
4. The program will compile correctly
What is the output for this code?
public class temp
{
public static void main(String agrs[])
{
for(int j=1; j<=10; j++);
System.out.print(j);
}
}
1. 12345678910
2. 11
3. error
4. 1 2 3 4 5 6 7 8 9 10
What will be the values of x and y, if n=1?

a=1;
b=1;
if(n>0)
a=a+1;
b=b-1;

1. a=1, b=1
2. a=0, b=2
3. a=2, b=1
4. a=2, b=0
Object of which class is used to compile regular
expression?

1. Pattern class
2. Matcher class
3. PatternSyntaxException
4. None of the mentioned
Which of the following matches nonword
character using regular expression in java?

1. \W
2. \w
3. \S
4. \s
Find the output of the following code.
public class Main
{
public static void main(String[] args) {
int x,y;
a = 2;
{
b = 4;
System.out.print(a + " " + b);
}
System.out.println(a+ " " + b);
}
}

1. 2 42 2
2. 2 4 42
3. Syntax error
4. Compilation Error
Find the output of the following code.
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
}
}

1. 324
2. 323
3. 234
4. 344
public class Main
{
public static void main(String[] args)
{
int x = 10;
if (++x < 10 && (x / 0 > 10)) {
System.out.println("Hello");
} else {
System.out.println("World");
}
}
}
1. 5
5.99f
2. 5
5.99
3. 5 5.99f
4. None of these
Find the output of the following code.
public class Main
{
public static void main(String[] args)
{
int myInt = 9;
double myDouble = myInt;
System.out.println(myInt);
System.out.println(myDouble);
}
}

1. 9 9.0
2. 9
9.0
3. 9.0
9
4. None of these
public class Main
{

public static void main(String argv[])


{
double n=5;
switch(n)
{
case 5:
System.out.println("10");
break;
case 5.0000000:
System.out.println("30");
break;
default:
System.out.println("0");
}
}
}
What are the entities whose values can be
changed called?

1. Constants
2. Variables
3. Modules
4. Tokens
class bitwise_operator
{
public static void main(String args[])
{
int a = 3;
int b = 6;
int c = a | b;
int d = a & b;
System.out.println(c + " " + d);
}
}
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c = 3;
int d = 10;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
int e = ~d;
System.out.println(a + " " + b + " " + c+ “ “+ e);
}
}
public class Main
{
public static void main(String[] args)
{
do
{
System.out.print(1);
do
{
System.out.print(2);

} while (false);

} while (false);
}
}
Options:
1. 12
2. 21
3. 1
4. 2
What does public int start() return?

1. returns start index of the input string


2. returns start index of the current match
3. returns start index of the previous match
4. none of the mentioned
What is the output of this program?
class comma_operator
{
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
a) 5
b) 6
c) 14
d) Compilation error
Find the output of the following code. public class Main
{
public static void main(String[] args)
{
int x = 10;
if (++x < 10 && (x / 0 > 10)) {
System.out.println("Hello");
} else {
System.out.println("World");
}
}
}

1. Hello
2. World
3. Hello World
4. None of the above mentioned
What will be the output of below statements?

String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");
System.out.println(s1==s2);
System.out.println(s1==s3);
Which of the following is not a class of
java.util.regex?

1. Pattern class
2. matcher class
3. PatternSyntaxException
4. Regex class
What will be the output of below statements?

String s1 = "abc";
String s2 = "def";
System.out.println(s1.compareTo(s2));
Find the output of following code.
public class Main
{
public static void main(String[] args)
{
int i = 0, j = 9;
do {
i++;
if (j-- < i++) {
break;
}
} while (i < 5);
System.out.println(i + " " + j);
}
}

1. 44
2. 55
3. 66
4. 77
What will be the output of below program?
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println("s1 == s2 is:" + s1 == s2); } }

a)false
b)s1 == s2 is:true
c)s1 == s2 is:false
d)true
What is the return type of String.compareTo()
method?
1. boolean
2. char
3. byte
4. int
What is the output of this program?
class output
{
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
c.delete(1,3);
System.out.println(c);
}
class output
{
public static void main(String args[])
{
String a = “Smart Java Program";
System.out.println(a.indexOf(‘J')+" "+a.indexOf('a')+"
"+a.lastIndexOf(‘s')+" "+a.lastIndexOf(‘a'));
}
}
Predict the output of the following code.
public class Test
{
public static void main(String[] args)
{
int i = 100;
long l = i;
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}

1. Int value 100


Long value 100
Float value 100.0

2. Int value 100


Long value 101
Float value 100.0

3. Int value 100


Long value 0100
Float value 100.0
class Output
{
public static void main(String args[])
{
int a = 5;
int b = 10;
first:
{
second:
{
third:
{
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}
Which of these methods can be used to writing
console output?
1. print()
2. println()
3. write()
4. All of the mentioned
Which of the following represents digits using
regular expression in java?

1. /d
2. /D
3. /s
4. /S
Generally string is a sequence of characters, But
in java, string is an_________?

1. Object
2. Class
3. Package
4. None of the above
public class A
{ public static class B {
public static void foo() { }
} } which statement is used to create an
instantiates an instance of the nested class?
1. A.B m = new A.B();
2. A.B mi = new B();
3. A m = new A();
A.B mi = m.new B();
4. B mi = new A.B();
class Test {
public static void main(String [] args) {
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}}

1. Small
2. Tiny
3. Huge
4. Compilation fails
The ………………… statement tests the value of a
given variable against a list of case values and
when a match is found, a block of statements
associated with that case is executed.
1. Switch
2. break
3. continue
4. default
What will be the output of below statements?
String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
System.out.println(s1.equals(s2));
What will be the output of below statements?

String s = "Java"+1+2+"Quiz"+""+(3+4);
System.out.println(s);

a)Java3Quiz7
b)Java12Quiz7
c)Java12Quiz34
d)Java3Quiz34
Find the output of the following program.
public class Test {
public static void main(String[] args)
{
String Branch = "CSE";
int year = 2;
switch (year) {
case 1:
System.out.println("elective courses : Advance english, Algebra");
break;
case 2:
switch (Branch)
{
case "CSE":
case "CCE":
System.out.println("elective courses : Machine Learning, Big Data");
break;
case "ECE":
System.out.println("elective courses : Antenna Engineering");
break;
default:
System.out.println("Elective courses : Optimization");
}
}
}
}
1. elective courses : Machine Learning, Big Data
2. elective courses : Optimization
3. elective courses : Antenna Engineering
4. None of these
public class jump_statments
{
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}

a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
By using …………………….., you can force
immediate termination of loop, bypassing the
conditional expression and any remaining code
in the body of the loop.
1. Switch
2. break
3. continue
4. default
Find the output of the following code.
class leftshift_operator
{
public static void main(String args[])
{
byte x = 64;
int i;
byte y;
i = x << 2;
y = (byte) (x << 2);
System.out.print(i + " " + y);
}
}
1. 0 64
2. 64 0
3. 0 256
4. 256 0
1) Which of the following can be used in a Java program as identifiers?
Check all of the identifiers that are legal.
1. _abc123 = 40;
2. abc_123$_= 10;
3. public class cda123$
4. 42abcd
5. ABC4
6. ABC
7. while
8. println

a) 1, 2
b) 1,2,3
c) 1,3,5,6
d) 1,2,3,5,6,8
Answer: d
2) What lines fail to compile?
public class MoviesAreFun {

public static void main(String[] args) {


int arya2 = 2; /*LINE A*/
double life_is_beautiful = 3.0; /*LINE B*/
char 7/gbrundavan_colony = 'S'; /*LINE C*/
short 3idiots = 255; /*LINE D*/
System.out.println(arya2);
System.out.println(life_is_beautiful);
System.out.println(7/gbrundavan_colony);
System.out.println(3idiots); }
}

a) ABCE
b) BC
c) CD
d) ABCD
Answer: c
3) Which of these is necessary condition for automatic type
conversion in Java?
a) The destination type is smaller than source type
b) The destination type is larger than source type
c) The destination type can be larger or smaller than source
type
d) None of the mentioned
Answer: b
4) What will be the output of the following Java program?
class array_output {
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
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
Answer: a
5. If an expression contains double, int, float,
long, then the whole expression will be
promoted into which of these data types?
a) long
b) int
c) double
d) float
Answer: c
6. class average {
public static void main(String args[])
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
System.out.print(result/6);

}
}
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
Answer: c
7) Which of these classes defined in java.io and used for file-
handling are abstract?
A. InputStream
B. PrintStream
C. Reader
D. FileInputStream

A. Only A
B. Only C
C. A and C
D. B and D
Answer: c
8) Which of these method of FileReader class is used to read
characters from a file?
a) read()
b) scanf()
c) get()
d) getInteger()
Answer: a
9) int x = 0, y = 0 , z = 0 ;
x = (++x + y-- ) * z++;

What will be the value of "x" after execution ?


A. -2
B. -1
C. 0
D. 1
E. 2
Answer: c
10) The order of precedence from highest to lowest is:

a) && , /, |, {}
b) %, <=, &&, =
c) <, !, ==, ++
d) +, -, [], !=
Answer: b
11) class Base {}
class Derived extends Base {
public static void main(String args[]){
Base a = new Derived();
System.out.println(a instanceof Derived);
}
}
a) True
b) False
c) Compile time error
d) Runtime error
Answer: a
Explanation: The instanceof operator works
even when the reference is of base class type.
12) Which of these selection statements test only for equality?
a) if
b) switch
c) if & switch
d) none of the mentioned
Answer: b
13) class selection_statements
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
a) 1
b) 2
c) 3
d) 4
Answer: b
14) class Output
{
public static void main(String args[])
{
int a = 5;
int b = 10;
first:
{ a) 5 10
second:
{
b) 10 5
third: c) 5
{ d) 10
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
Answer: d
15) Which of the following loops will execute
the body of loop even when condition
controlling the loop is initially false?
a) do-while
b) while
c) for
d) none of the mentioned
Answer: a
16) Which of these jump statements can skip
processing the remainder of the code in its
body for a particular iteration?
a) break
b) return
c) exit
d) continue
Answer: d
17) Which of this statement is incorrect?
a) switch statement is more efficient than a set
of nested ifs
b) two case constants in the same switch can
have identical values
c) switch statement can only test for equality,
whereas if statement can evaluate any type of
boolean expression
d) it is possible to create a nested switch
statements
Answer: b
18) Which of this method of class String is
used to obtain a length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
Answer: d
Explanation: Method length() of string class is
used to get the length of the object which
invoked method length().
19) public class Test{ public static void main(String
args[]){ String s1 = "SITHA"; String s2 = "RAMA";
System.out.println(s1.charAt(0) > s2.charAt(0)); } }

A. true
B. false
C. 0
D. Compilation error
E. Throws Exception
Answer: a
20) public class Test{ public static void
main(String args[]){ String x = "hellow"; int y =
9; System.out.println(x += y); } }

A. Throws an exception as string and int are not


compatible for addition
B. hellow9
C. 9hellow
D. Compilation error
E. None of these
Answer: B
21) What is the output of the following println
statement?String str1 = "Hellow";
System.out.println(str1.indexOf('t'));
A. true
B. false
C. 1
D. -1
Answer: d
22) Which of the following is not a class of
java.util.regex?
a) Pattern class
b) matcher class
c) PatternSyntaxException
d) Regex class
Answer: d
Explanation: java.util.regex consists 3 classes.
PatternSyntaxException indicates syntax
error in regex.
23) Which of the following matches nonword
character using regular expression in java?
a) \w
b) \W
c) \s
d) \S
Answer: b
Explanation: \W matches nonword
characters. [0-9], [A-Z] and _ (underscore) are
word characters. All other than these
characters are nonword characters.
24) Which of the following matches end of the
string using regular expression in java?
a) \z
b) \\
c) \*
d) \Z
Answer: a
Explanation: \z is used to match end of the
entire string in regular expression in java.
25) import java.util.regex.*;
public class RegularEx {
public static void main(String... arg) {
Pattern pattern = Pattern.compile("M+", 5);
Matcher matcher = pattern.matcher("M Merit Match MM m mM");
while (matcher.find())
System.out.print(matcher.group() + " ");
}
}

a) M M M MM M
b) M M M MM m mM
c) M M M MM mM
d) PatternSyntaxException
Answer: A

S-ar putea să vă placă și