Thầy cho 2 đề bài lí thuyết java để ôn tập mà trình anh văn mình gà lắm ! Mới làm dc 1 số câu thôi ak mà k0 bik đúng hay sai ! Ai giỏi anh văn giải giúp mình với nhé ! Thanks a e nhìu !

[AH]Đề 1:

Question 1)

Which of the following lines will compile without warning or error.

1) float f=1.3;

2) char c="a"; (dap an minh chon)

3) byte b=257;

4) boolean b=null; (dap an minh chon)

5) int i=10;

Question 2)

What will happen if you try to compile and run the following code

public class MyClass {

public static void main(String arguments[]) {

amethod(arguments);

}

public void amethod(String[] arguments) {

System.out.println(arguments);

System.out.println(arguments[1]);

}

}

1) error Can't make static reference to void amethod. ( dap an minh chon )

2) error method main not correct

3) error array must include parameter

4) amethod must be declared with String

Question 3)

Which of the following will compile without error

1)

import java.awt.*;

package Mypackage;

class Myclass {}

2)

package MyPackage;

import java.awt.*; (dap an minh chon)

class MyClass{}

3)

/*This is a comment */

package MyPackage;

import java.awt.*;

class MyClass{}

Question 4)

A byte can be of what size

1) -128 to 127

2) (-2 power 8 )-1 to 2 power 8

3) -255 to 256

4)depends on the particular implementation of the Java Virtual machine

Question 5)

What will be printed out if this code is run with the following command line?

java myprog good morning

public class myprog{

public static void main(String argv[])

{

System.out.println(argv[2])

}

}

1) myprog

2) good

3) morning

4) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2" (dap an minh chon)

Question 6)

Which of the following are keywords or reserved words in Java?

1) if (dap an minh chon)

2) then

3) goto

4) while

5) case (dap an minh chon)

Question 7)

Which of the following are legal identifiers

1) 2variable

2) variable2

3) _whatavariable

4) _3_

5) $anothervar

6) #myvar

Question Cool

What will happen when you compile and run the following code?

public class MyClass{

static int i;

public static void main(String argv[]){

System.out.println(i);

}

}

1) Error Variable i may not have been initialized

2) null

3) 1

4) 0 (dap an minh chon)

Question 9)

What will happen if you try to compile and run the following code?

public class Q {

public static void main(String argv[]){

int anar[]=new int[]{1,2,3};

System.out.println(anar[1]);

}

}

1) 1

2) Error anar is referenced before it is initialized

3) 2 (dap an minh chon )

4) Error: size of array must be defined

Question 10)

What will happen if you try to compile and run the following code?

public class Q {

public static void main(String argv[]){

int anar[]=new int[5];

System.out.println(anar[0]);

}

}

1) Error: anar is referenced before it is initialized

2) null

3) 0 (dap an minh chon)

4) 5

Question 11)

What will be the result of attempting to compile and run the following code?

abstract class MineBase {

abstract void amethod();

static int i;

}

public class Mine extends MineBase {

public static void main(String argv[]){

int[] ar=new int[5];

for(i=0;i < ar.length;i++)

System.out.println(ar);

}

}

1) a sequence of 5 0's will be printed

2) Error: ar is used before it is initialized

3) Error Mine must be declared abstract

4) IndexOutOfBoundes Error

Question 12)

What will be printed out if you attempt to compile and run the following code ?

int i=1;

switch (i) {

case 0:

System.out.println("zero");

break;

case 1:

System.out.println("one");

case 2:

System.out.println("two");

default:

System.out.println("default");

}

1) one

2) one, default

3) one, two, default

4) default

Question 13)

What will be printed out if you attempt to compile and run the following code?

int i=9;

switch (i) {

default:

System.out.println("default");

case 0:

System.out.println("zero");

break;

case 1:

System.out.println("one");

case 2:

System.out.println("two");

}

1) default

2) default, zero

3) error default clause not defined

4) no output displayed

Question 14)

Which of the following lines of code will compile without error

1)

int i=0;

if(i) {

System.out.println("Hello");

}

2)

boolean b=true;

boolean b2=true;

if(b==b2) {

System.out.println("So true");

}

3)

int i=1;

int j=2;

if(i==1|| j==2)

System.out.println("OK");

4)

int i=1;

int j=2;

if(i==1 &| j==2)

System.out.println("OK");

Question 15)

Which of the following statements are true?

1) Methods cannot be overriden to be more private

2) Static methods cannot be overloaded

3) Private methods cannot be overloaded

4) An overloaded method cannot throw exceptions not checked in the base class

Question 16)

What will happen if you attempt to compile and run the following code?

Integer ten=new Integer(10);

Long nine=new Long (9);

System.out.println(ten + nine);

int i=1;

System.out.println(i + ten);

1) 19 followed by 20

2) 19 followed by 11

3) Error: Can't convert java lang Integer

4) 10 followed by 1

Question 17)

What will happen if you attempt to compile and run the following code?

1) Compile and run without error

2) Compile time Exception

3) Runtime Exception

class Base {}

class Sub extends Base {}

class Sub2 extends Base {}

public class CEx{

public static void main(String argv[]){

Base b=new Base();

Sub s=(Sub) b;

}

}

Question 18)

Which of the following statements are true?

1) System.out.println( -1 >>> 2);will output a result larger than 10

2) System.out.println( -1 >>> 2); will output a positive number

3) System.out.println( 2 >> 1); will output the number 1

4) System.out.println( 1 <<< 2); will output the number 4

Question 19)

Which of the following methods can be legally inserted in place of the comment //Method Here ?

class Base{

public void amethod(int i) { }

}

public class Scope extends Base{

public static void main(String argv[]){

}

//Method Here

}

1) void amethod(int i) throws Exception {}

2) void amethod(long i)throws Exception {}

3) void amethod(long i){}

4) public void amethod(int i) throws Exception {}

Question 20)

What is the result of the following operation?

System.out.println(4 | 3);

1) 6

2) 0

3) 1

4) 7

[/AH]

[AH]Đề 2 :

1. Which declaration of the main method below would allow a class to be started as a standalone program? Select the one correct answer.

A. public static int main(char args[])

B. public static void main(String args[]) (dap an minh chon)

C. public static void MAIN(String args[])

D. public static void main(String args)

E. public static void main(char args[])

2. What all gets printed when the following code is compiled and run? Select the three correct answers.

public class xyz {

public static void main(String args[]) {

for(int i = 0; i < 2; i++) {

for(int j = 2; j>= 0; j--) {

if(i == j) break;

System.out.println("i=" + i + " j="+j);

}

}

}

}

A. i=0 j=0

B. i=0 j=1 (dap an minh chon)

C. i=0 j=2 (dap an minh chon)

D. i=1 j=0

E. i=1 j=1

F. i=1 j=2 (dap an minh chon)

G. i=2 j=0

H. i=2 j=1

I. i=2 j=2

3. What gets printed when the following code is compiled and run with the following command - java test 2

Select the one correct answer.

public class test {

public static void main(String args[]) {

Integer intObj=Integer.valueOf(args[args.length-1]);

int i = intObj.intValue();

if(args.length > 1)

System.out.println(i);

if(args.length > 0)

System.out.println(i - 1);

else

System.out.println(i - 2);

}

}

A. test (dap an minh chon)

B. test -1

C. 0

D. 1

E. 2

4. Which of the following is a Java keyword. Select the four correct answers.

A. extern (dap an minh chon)

B. synchronized

C. volatile

D. friend

E. friendly

F. transient

G. this (dap an minh chon)

H. then

5. Is the following statement true or false. The constructor of a class must not have a return type.

A. true

B. false

6. What is the number of bytes used by Java primitive long. Select the one correct answer.

A. The number of bytes is compiler dependent.

B. 2

C. 4

D. 8

E. 64

7. What is returned when the method substring(2, 4) of the String "Example".

A. “am”

B. “amp”

C. “ampl”

D. “xa”

8. What is the result of evaluating the expression 14 | 13. Select the one correct answer.

A. 14

B. 13

C. 27

D. 15

9. Which of the following is true. Select the two correct answers.

A. A class that is abstract may not be instantiated.

B. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.

C. A static variable indicates there is only one copy of that variable.

D. A method defined as private indicates that it is accessible to all other classes in the same package.

10. What all gets printed when the following program is compiled and run. Select the two correct answers.

public class test {

public static void main(String args[]) {

int i, j=1;

i = (j>1)?2:1;

switch(i) {

case 0: System.out.println(0); break;

case 1: System.out.println(1);

case 2: System.out.println(2); break;

case 3: System.out.println(3); break;

}

}

}

A. 0

B. 1 (dap an minh chon)

C. 2 (dap an minh chon)

D. 3

11. What all gets printed when the following program is compiled and run. Select the one correct answer.

public class test {

public static void main(String args[]) {

int i=0, j=2;

do {

i=++i;

j--;

} while(j>0);

System.out.println(i);

}

}

A. 0

B. 1

C. 2 (dap an minh chon)

D. The program does not compile because of statement "i=++i;"

12. What all gets printed when the following gets compiled and run. Select the three correct answers.

public class test {

public static void main(String args[]) {

int i=1, j=1;

try {

i++;

j--;

if(i/j > 1)

i++;

}

catch(ArithmeticException e) {

System.out.println(0);

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println(1);

}

catch(Exception e) {

System.out.println(2);

}

finally {

System.out.println(3);

}

System.out.println(4);

}

}

A. 0 (dap an minh chon)

B. 1

C. 2

D. 3 (dap an minh chon)

E. 4 (dap an minh chon)

13. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test {

public static void main(String args[]) {

int i=1, j=1;

try {

i++;

j--;

if(i == j)

i++;

}

catch(ArithmeticException e) {

System.out.println(0);

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println(1);

}

catch(Exception e) {

System.out.println(2);

}

finally {

System.out.println(3);

}

System.out.println(4);

}

}

A. 0

B. 1

C. 2

D. 3 (dap an minh chon)

E. 4 (dap an minh chon)

14. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test {

public static void main(String args[]) {

String s1 = "abc";

String s2 = "abc";

if(s1 == s2)

System.out.println(1);

else

System.out.println(2);

if(s1.equals(s2))

System.out.println(3);

else

System.out.println(4);

}

}

A. 1 (dap an minh chon)

B. 2

C. 3 (dap an minh chon)

D. 4

15. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test {

public static void main(String args[]) {

String s1 = "abc";

String s2 = new String("abc");

if(s1 == s2)

System.out.println(1);

else

System.out.println(2);

if(s1.equals(s2))

System.out.println(3);

else

System.out.println(4);

}

}

A. 1

B. 2 (dap an minh chon)

C. 3 (dap an minh chon)

D. 4

16. Which of the following are legal array declarations. Select the three correct answers.

A. int i[5][];

B. int i[][];

C. int []i[];

D. int i[5][5];

E. int[][] a;

17. What gets printed when the following code is compiled and run. Select the one correct answer.

public class test {

public static void main(String args[]) {

int i = 1;

do {

i--;

} while (i > 2);

System.out.println(i);

}

}

A. 0 (dap an minh chon)

B. 1

C. 2

D. –1

18. Which of the following are legal identifier names in Java. Select the two correct answers.

A. %abcd

B. $abcd

C. 1abcd

D. package

E. _a_long_name

19. String s = new String("xyz");

Assuming the above declaration, which of the following statements would compile. Select the one correct answer.

A. s = 2 * s;

B. int i = s[0];

C. s = s + s;

D. s = s >> 2;

E. None of the above.

20. In which all cases does an exception gets generated. Select the two correct answers.

int i = 0, j = 1;

A. if((i == 0) || (j/i == 1))

B. if((i == 0) | (j/i == 1))

C. if((i != 0) && (j/i == 1))

D. if((i != 0) & (j/i == 1))

[/AH]