0% found this document useful (0 votes)
463 views

JAVA Basic

The document contains 27 multiple choice questions about Java programming concepts such as data types, arrays, operators, and method overloading. The questions cover topics like primitive data types, type conversion, output of code snippets, and errors from invalid code. Correct answers are provided for each question.

Uploaded by

suraj raut
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
463 views

JAVA Basic

The document contains 27 multiple choice questions about Java programming concepts such as data types, arrays, operators, and method overloading. The questions cover topics like primitive data types, type conversion, output of code snippets, and errors from invalid code. Correct answers are provided for each question.

Uploaded by

suraj raut
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1. Java is a ........... language.

A. weakly typed B. strongly typed     C. moderate typed D. None of these

Answer: Option B

2. How many primitive data types are there in Java?

A. 6 B. 7 C. 8 D. 9

Answer: Option C

3. In Java byte, short, int and long all of these are

A. signed B. unsigned C. Both of the above D. None of these

Answer: Option A

4. Size of int in Java is

A. 16 bit B. 32 bit C. 64 bit D. Depends on execution environment

Answer: Option B

5. The smallest integer type is ......... and its size is ......... bits.

A. short, 8 B. byte, 8 C. short, 16 D. short, 16

Answer: Option B

6. Size of float and double in Java is

A. 32 and 64 B. 64 and 64 C. 32 and 32 D. 64 and 32

Answer: Option A

7. Determine output:

class A{
public static void main(String args[]){
int x;
x = 10;
if(x == 10){
int y = 20;
System.out.print("x and y: "+ x + " " + y);
y = x*2;
}
y = 100;
System.out.print("x and y: " + x + " " + y);
}
}

A. 10 20 10 100 B. 10 20 10 20

C. 10 20 10 10 D. Error

Answer: Option D

8. Automatic type conversion in Java takes place when

A. Two type are compatible and size of destination type is shorter than source type.

B. Two type are compatible and size of destination type is equal of source type.

C. Two type are compatible and size of destination type is larger than source type.

D. All of the above

Answer: Option C

9. Which of the following automatic type conversion will be possible?

A. short to int B. byte to int

C. int to long D. long to int

Answer: Option C

10. What is the output of the following program?

class A{
public static void main(String args[]){
byte b;
int i = 258;
double d = 325.59;

b = (byte) i;
System.out.print(b);

i = (int) d;
System.out.print(i);
b = (byte) d;
System.out.print(b);
}
}

A. 258 325 325 B. 258 326 326

C. 2 325 69 D. Error

Answer: Option C

11. Determine output:

public class Test {


static void test(float x){
System.out.print("float");
}

static void test(double x){


System.out.print("double");
}

public static void main(String[] args){


test(99.9);
}
}

A. float B. double

C. Compilation Error D. Exception is thrown at runtime

Answer: Option B

12. The following fraction of code

double STATIC = 2.5 ;


System.out.println( STATIC );

A. Prints 2.5

B. Rraises an error as STATIC is used as a variable which is a keyword

C. Raises an exception D. None of these

Answer: Option A

13. What would be the output of the following fraction of code ?

int Integer = 34 ;
char String = 'S' ;
System.out.print( Integer ) ;
System.out.print( String ) ;

A. Does not compile as Integer and String are API class names.

B. Throws exception. C. 34 D. S E. 34 S

Answer: Option E

14. What is the output of the following program?

public class Test{


static int x = 10 ;
public static void main(String[] a){
Test test = new Test( ) ;
Test test1 = new Test( ) ;
test.x += 1 ;
System.out.println( test.x + test1.x ) ;
}
}

A. 20 B. 21 C. 22 D. Compilation Error

E. Throws Exception

Answer: Option C

15. What will be output of the following program code?

public class Test{


public static void main(String[] a){
short x = 10;
x = x*5;
System.out.print(x);
}
}

A. 50 B. 10 C. Compilation Error D. None of these

Answer: Option C

16. Determine output:

public class Test{


int i = 34;
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.i = 65;
System.out.print(t1.i);
System.out.print(t2.i);
}
}

A. 34 34 B. 65 34 C. 65 65 D. 34 65
Answer: Option B

17. The following program:

public class Test{


static boolean isOK;
public static void main(String args[]){
System.out.print(isOK);
}
}

A. Prints true B. Prints false

C. Will not compile as boolean is not initialized

D. Will not compile as boolean can never be static

Answer: Option B

18. In Java, the word true is ................

A. A Java keyword B. A Boolean literal

C. Same as value 1 D. Same as value 0

Answer: Option B

19. What will the output of the following program?

public class Test{


public static void main(String args[]){
float f = (1 / 4) * 10;
int i = Math.round(f);
System.out.println(i);
}
}

A. 2 B. 0 C. 3

D. 2.5 E. 25

Answer: Option B

20. What is the output for the below code ?

class A{
int k;
boolean istrue;
static int p;
public void printValue(){
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}

public class Test{


public static void main(String argv[]){
A a = new A();
a.printValue();
}
}

A. 0 false 0 B. 0 true 0 C. 000

D. Compile error - static variable must be initialized before use. E. None of these

Answer: Option A

21. What is the output for the below code?

public class Test{


int _$;
int $7;
int do;

public static void main(String argv[]){


Test test = new Test();
test.$7=7;
test.do=9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}

A. 790 B. 700

C. Compile error - $7 is not valid identifier.

D. Compile error - do is not valid identifier. E. None of these

Answer: Option D

22. What is the output for the below code ?

1. public class Test{


2. public static void main(String[] args){
3. int i = 010;
4. int j = 07;
5. System.out.println(i);
6. System.out.println(j);
7. }
8. }
A. 87 B. 10 7

C. Compilation fails with an error at line 3

D. Compilation fails with an error at line 5 E. None of these

Answer: Option A

23. What is the output for the below code ?

1. public class Test{


2. public static void main(String[] args){
3. byte b = 6;
4. b+=8;
5. System.out.println(b);
6. b = b+7;
7. System.out.println(b);
8. }
9. }

A. 14 21 B. 14 13

C. Compilation fails with an error at line 6

D. Compilation fails with an error at line 4 E. None of these

Answer: Option C

24. What will be the output for the below code ?

1. public class Test{


2. public static void main(String[] args){
3. byte i = 128;
4. System.out.println(i);
5. }
6. }

A. 128 B. 0 C. Compilation fails with an error at line


3

D. Compilation fails with an error at line 4 E. None of these

Answer: Option C

25. What will be the output for the below code ?

1. public class Test{


2. int i=8;
3. int j=9;
4. public static void main(String[] args){
5. add();
6. }
7. public static void add(){
8. int k = i+j;
9. System.out.println(k);
10. }
11. }

A. 17 B. 0 C. Compilation fails with an error at line 5

D. Compilation fails with an error at line 8 E. None of these

Answer: Option D

26. What will be output of following program?

public class Test{


public static void main(String[] args){
byte b=127;
b++;
b++;
System.out.println(b);
}
}

A. 2 B. 129     C. -127 D. Compiler error

E. None of these

Answer: Option C

27. Determine output:

public class Test{


int a = 10;

public void method(int a){


a += 1;
System.out.println(++a);
}
public static void main(String args[]){
Test t = new Test();
t.method(3);
}
}

A. 4 B. 5 C. 12 D. 11

E. None of these

Answer: Option B
1. In Java arrays are

A. objects B. object references C. primitive data type

D. None of the above

Answer: Option A

2. Which one of the following is a valid statement?

A. char[] c = new char(); B. char[] c = new char[5];

C. char[] c = new char(4); D. char[] c = new char[];

Answer: Option B

3. What is the result of compiling and running the following code?

public class Test{


public static void main(String[] args){
int[] a = new int[0];
System.out.print(a.length);
}
}

A. 0

B. Compilation error, arrays cannot be initialized to zero size.

C. Compilation error, it is a.length() not a.length

D. None of the above

Answer: Option A

4. What will be the output?

public class Test{


public static void main(String[] args){
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}

A. The program has a compile error because the size of the array wasn't specified when
declaring the array.
B. The program has a runtime error because the array elements are not initialized.

C. The program runs fine and displays x[0] is 0.

D. The program has a runtime error because the array element x[0] is not defined.

Answer: Option C

5. What is the output of the following code?

public class Test{


public static void main(String args[]){
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for(int i = 1; i < myList.length; i++){
if(myList[i] > max){
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
}
}

A. 0 B. 1

C. 2 D. 3 E. 4

Answer: Option B

6. What is output of the following code:

public class Test{


public static void main(String[] args){
int[] x = {120, 200, 016 };
for(int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}

A. 120 200 16 B. 120 200 14

C. 120 200 016 D. 016 is a compile error. It should be written as 16.

Answer: Option B

7. Determine output:

public class Test{


public static void main(String[] args){
int[] x = {1, 2, 3, 4};
int[] y = x;

x = new int[2];

for(int i = 0; i < x.length; i++)


System.out.print(y[i] + " ");
}
}

A. 1234 B. 0000

C. 12 D. 00 E. None of these

Answer: Option C

8. Analyze the following code and choose the correct answer.

int[] arr = new int[5];


arr = new int[6];

A. The code has compile errors because the variable arr cannot be changed once it is
assigned.

B. The code has runtime errors because the variable arr cannot be changed once it is
assigned.

C. The code can compile and run fine. The second line assigns a new array to arr.

D. The code has compile errors because we cannot assign a different size array to arr.

Answer: Option C

9. What will be the output?

public class Test{


public static void main(String[] args){
int[] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}

A. The program has a compile error because new int[2<sp< label=""> </sp<>

B. The program has a runtime error because a[1 C. a[1] is 0

D. a[1] is 1
Answer: Option C

10. When you pass an array to a method, the method receives ________ .

A. A copy of the array. B. A copy of the first element.

C. The reference of the array. D. The length of the array.

Answer: Option C

11. What would be the result of attempting to compile and run the following code?

public class HelloWorld{


public static void main(String[] args){
double[] x = new double[]{1, 2, 3};
System.out.println("Value is " + x[1]);
}
}

A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and
it should be replaced by {1, 2, 3}.

B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[3]{1, 2, 3};

C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[]{1.0, 2.0, 3.0};

D. The program compiles and runs fine and the output

Answer: Option D

12. Which will legally declare, construct, and initialize an array?

A. int [] myList = {}; B. int [] myList = (5, 8, 2);

C. int myList [] [] = {4,9,7,0}; D. int myList [] = {4, 3, 7};

Answer: Option D

13. What will be the output of the program?

public class Test{


public static void main(String [] args){
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}

and the command-line invocation is C:Java> java Test 1 2 3 4

A. args[2] = 2 B. args[2] = 3 C. args[2] = null

D. An exception is thrown at runtime.

Answer: Option D

14. What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};


for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

A. 0 B. 1 C. 2

D. 3 E. 4

Answer: Option B

1. The output of the following fraction of code is

public class Test{


public static void main(String args[]){
String s1 = new String("Hello");
String s2 = new String("Hellow");
System.out.println(s1 = s2);
}
}

A. Hello B. Hellow C. Compilation error

D. Throws an exception E. None of these

Answer: Option B

2. What will be the output of the following program code?

class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}
A. true B. false C. 0

D. 1 E. Displays error message

Answer: Option B

3. What will be the output of the following program?

public class Test{


public static void main(String args[]){
String s1 = "java";
String s2 = "java";
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
}
}

A. false true B. false false C. true false

D. true true

Answer: Option D

4. Determine output:

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: Option A

5. What could be output of the following fragment of code?

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: Option B

6. toString() method is defined in

A. java.lang.String B. java.lang.Object

C. java.lang.util D. None of these

Answer: Option B

7. The String method compareTo() returns

A. true B. false C. an int value

D. 1 E. -1

Answer: Option C

8. What will be the output?

String str1 = "abcde";


System.out.println(str1.substring(1, 3));

A. abc B. bc C. bcd

D. abcd E. None of these

Answer: Option B

9. 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 E. 0

Answer: Option D
10. What will be the output of the following program?

public class Test{


public static void main(String args[]){
String str1 = "one";
String str2 = "two";
System.out.println(str1.concat(str2));
}
}

A. one B. two C. onetwo

D. twoone E. None of these

Answer: Option C

11. String str1 = "Kolkata".replace('k', 'a');

In the above statement, the effect on string Kolkata is

A. The first occurrence of k is replaced by a. B. All characters k are replaced by a.

C. All characters a are replaced by k. D. Displays error message

Answer: Option B

12. The class string belongs to ................. package.

A. java.awt B. java.lang C. java.applet

D. java.string

Answer: Option B

13. What will be the output?

public class Test{


public static void main (String[] args){
String test = "a1b2c3";
String[] tokens = test.split("\\d");
for(String s: tokens)
System.out.print(s);
}
}

A. abc B. 123 C. Runtime exception thrown

D. Compilation error
Answer: Option A

14. How many objects will be created?

String a = new String("Examveda");


String b = new String("Examveda");
String c = "Examveda";
String d = "Examveda";

A. 4 B. 3 C. 2 D. None of this

Answer: Option B

15. How many Constructor String class have?

A. 2 B. 7 C. 9 D. 11

E. None of this

Answer: Option D

16. What will be output?

String S1 = "S1 ="+ "123"+"456";


String S2 = "S2 ="+(123+456);

A. S1=123456, S2=579 B. S1=123456,S2=123456

C. S1=579,S2=579 D. None of This

Answer: Option A

17. What will be the output of the following program code?

public class Test{


public static void main(String args[]){
String s = "what";
StringBuffer sb = new StringBuffer("what");
System.out.print(sb.equals(s)+","+s.equals(sb));
}
}

A. true,true B. false,true

C. true,false D. false,false E. None of these

Answer: Option D

18. What will be the output?


1. public class Test{
2. public static void main(String args[]){
3. Object myObj = new String[]{"one", "two", "three"};
4. {
5. for(String s : (String[])myObj)
6. System.out.print(s + ".");
7. }
8. }
9. }

A. one.two.three. B. Compilation fails because of an error at line 3

C. Compilation fails because of an error at line 5

D. An exception is thrown at runtime. E. None of these

Answer: Option A

19. Determine output:

public class Test{


public static void main(String args[]){
String str = null;
if(str.length() == 0){
System.out.print("1");
}
else if(str == null){
System.out.print("2");
}
else{
System.out.print("3");
}
}
}

A. Compilation fails. B. "1" is printed.

C. "2" is printed. D. "3" is printed.

E. An exception is thrown at runtime.

Answer: Option E

1. Determine output:

public class Test{


public static void main(String args[]){
int i;
for(i = 1; i < 6; i++){
if(i > 3) continue ;
}
System.out.println(i);
}
}
A. 2 B. 3 C. 4 D. 5

E. 6

Answer: Option E

2. In java, ............ can only test for equality, where as .......... can evaluate any type of the
Boolean expression.

A. switch, if B. if, switch C. if, break

D. continue, if

Answer: Option A

3. What will be the output of the following program?

public class Test{


public static void main(String args[]){
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ){
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
}

A. 06172838 B. 06172839

C. 06152535 D. Compilation Error

Answer: Option A

4. Consider the following program written in Java.

class Test{
public static void main(String args[]){
int x=7;
if(x==2); // Note the semicolon
System.out.println("NumberSeven");
System.out.println("NotSeven");
}
}

What would the output of the program be?

A. NumberSeven NotSeven B. NumberSeven


C. NotSeven D. Error

E. 7

Answer: Option A

5. Determine output:

public class Test{


public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
System.out.println(i);
}
}

A. 10 B. 11 C. 9 D. 20

E. None of these

Answer: Option A

6. What will be the output?

public class Test{


public static void main(String[] args){
int x=10, y=0;
if(x && y){
System.out.print("TRUE");
}
else{
System.out.print("FALSE");
}
}
}

A. FALSE B. TRUE C. Compilation Error

D. Runtime Error

Answer: Option C

7. What will be the value of y after execution of switch statement?

public class Test{


public static void main(String[] args){
int x = 3, y = 4;
switch(x + 3){
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
}
}

A. 1 B. 2 C. 3 D. 4 E. 0

Answer: Option B

8. What is the printout of the following switch statement?

char ch = 'a';
switch (ch){
case 'a':
case 'A': System.out.print(ch); break;
case 'b':
case 'B': System.out.print(ch); break;
case 'c':
case 'C': System.out.print(ch); break;
case 'd':
case 'D': System.out.print(ch);
}

A. abcd B. aa C. a D. ab E. abc

Answer: Option C

9. How many times will the following code print "Welcome to Examveda"?

int count = 0;
do {
System.out.println("Welcome to Examveda");
count++;
} while (count < 10);

A. 8 B. 9 C. 10 D. 11 E. 0

Answer: Option C

10. Choose the correct statement in context of the following program code.

public class Test{


public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}

A. The program has a compile error because the adjustment is missing in the for loop.

B. The program has a compile error because the control variable in the for loop cannot be of
the double type.

C. The program runs in an infinite loop because d<10 would always be true.
D. The program compiles and runs fine.

Answer: Option D

11. Which of the following for loops will be an infinite loop?

A. for(; ;) B. for(i=0 ; i<1; i--) C. for(i=0; ; i++)

D. All of the above

Answer: Option D
To view a Sol

12.What will be the result of the following code?

public class Test{


static public void main(String args[]){ //line 2
int i, j;
for(i=0; i<3; i++){
for(j=1; j<4; j++){
i%=j;
System.out.println(j);
}
}
}
}

A. 1231 B. 1232

C. Repeatedly print 1 2 3 and cause infinite loop.

D. Compilation fails because of line 2 E. None of these

Answer: Option C

What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};


for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

A. 0 B. 1 C. 2 D. 3 E. 4

Answer: Option B

14. What will be the result of compiling and runnig the following code:

public class Test{


public static void main(String... args) throws Exception{
Integer i = 34;
int l = 34;
if(i.equals(l)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}

A. true B. false C. Compiler error D. None of these

Answer: Option A

15. What all gets printed when the following program is compiled and run.

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 C. 2 D. 3 E. 12

Answer: Option E

16. What all gets printed when the following program is compiled and run?

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

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

Answer: Option C

17. What will be the output?

public class Test{


public static void main(String args[]){
int i = 1;
do{
i--;
}while(i > 2);
System.out.println(i);
}
}

A. 1 B. 2 C. -1 D. 0

E. None of these

Answer: Option D

18. Which option, inserted at line 4, produces the output 12?

1. public class Test{


2. public static void main(String [] args){
3. int x = 0;
4. // insert code here
5. do{ } while(x++ < y);
6. System.out.println(x);
7. }
8. }

A. int y = x; B. int y = 10; C. int y = 11;

D. int y = 12; E. None of the above will allow compilation to succeed.

Answer: Option C

19. What will be the result?

1. int i = 10;
2. while(i++ <= 10){
3. i++;
4. }
5. System.out.print(i);

A. 10 B. 11 C. 12 D. 13

E. Line 5 will be never reached.

Answer: Option D

You might also like