pacman, rainbows, and roller s
HomeAbout UsTutorialsHandouts Exams QnA Latest Info

InfoMAS

Questions and Answers for CPT 221

Introduction to JAVA


NOW YOU CAN TYPE AND RUN YOUR JAVA CODE ONLINE USING YOUR MOBILE!

Cod from anywhere with your mobile .Begin now and start learning and Programming language of your choice using your mobile! NO TIME!!!


ALSO TEST RUN ALL THE PROVIDED HERE IN THIS Q&A on line wit your MOBILE


Explore with your mobile and build yourself up in programming with no Laptop, Desktop BUT ALL ON YOUR MOBILE

Click on this column to start.

 

This Q&A is provided and intended to be taking as quiz.

 

*Support InfoMAS:

If you appreciate what the InfoMAS is doing, consider gifting the InfoMAS with airtime recharge.  That will help us to subscribe for data bundles and enable us to upload more of your course materials for download. Our dedicated mobile/modem number is MTN 08141902333.  Together, let's make a difference.  Don't like this? No problem proceed with CPT221 Q&A below


WARNING: The questions provided here are selected from previously and Frequently Asked Questions on CPT221 and should NOT be taking for questions that will be asked in your CA. Using the FAQ analysis in selecting the questions, there are possibilities though of coming across similar questions here in your CA BUT our aim is to give everyone the opportunity to practice and understand the subject under discuss rather than relying on EXPOnents for your success.


To compile and run any of the code found in this Q&A select and copy the source code and paste it in your notepad. Save it with the name InfoMAS.java open your command prompt and type the following;

1. javac InfoMAS.java 

2. java InfoMAS

Line 1: compile the program

Line 2: runs the program

Remember to save the file to the current directory/location of your system


1. Which of these operators is used to allocate memory to array variable in Java?

a) malloc

b) alloc

c) new

 Answer: c

Explanation: Operator new allocates block of memory specified by the size of array, and gives the reference of memory allocated to the array variable.

2. Which of these is an incorrect array declaration?

a) int arr[] = new int[5]

b) int [] arr = new int[5]

c) arr = new int[5]

d) arr[] = int [5] new

 Answer: d

Explanation: The new operator must come before the type

3. What is the output of this program?


 class InfoMAS

{

public static void main(String args[ ])

{

int cpt221[] = new int[10]

for(int i = 0; i<10; i++)

{

cpt221(i) = i;

system.out.print(cpt221[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

 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.

 4. What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int cpt221[] = new int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int n = 6;

n = cpt221[cpt221[n] / 2];

system.out.print(cpt221[n] / 2);

    }

}


a) 3

b) 0

c) 6

d) 1

 Answer: d

Explanation: Array arr contains 10 elements. N contains 6 thus in next line n is given value 2 printing arr[2] /2 i. e 2/2 = 1.

 

 

5. What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int array_variable[ ][ ] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int sum = 0;

for(int i = 0; i<3; i++)

for(int j = 0; j<3; ++j)

sum = sum + array_variable[i][j];

system.out.print(sum/5);

          }

     }


a) 8

b) 9

c) 10

d) 11

 Answer: d

Explanation: none

  

6. Which of these selection statements test only for equality?

a) if

b) switch

c) if & switch

d) None of the mentioned 

Answer: b

Explanation: switch statements for equality between the controlling variable and its constant

 

7. Which of these are selection statements in Java?

a) if()

b) for()

c) continue

d) break 

Answer: a

Explanation: continue and break are jump statements, and for is a looping statement

 

8. 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

Explanation: None

 

9. Which of these jump statements can skip processing remainder of code in its body for a particular iteration?

a) break

b) return

c) exit

d) continue

 

Answer: d

Explanation: None.

 

10.  Which of these values can a Boolean variable contain?

a) True & False

b) 0 & 1

c) Any integer value

d) true

 

Answer: a

Explanation: Boolean variable can contain only one of two possible values, true and false.

 

11. Which one is a valid declaration of a Boolean?

a) boolean b1 = 1;

b) boolean b2 = "false"

c) boolean b3 = false;

d) boolean b4 = "true"

Answer: c

Explanation: Boolean can only be assigned true or false literals. Remember you do not need to put Boolean values (True/False) in quote so that Java will not treat them as strings. That perfectly explain why option b is not correct.

 

12. What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

char a = 'A';

a++;

System.out.print ((int) a);

         }

     }


a) 66

b) 67

c) 65

d) 64

Answer: a

Explanation: Yes! I know you want an explanation on this definitely, you see ASCII value of character 'A' is 65 and this value is store in variable a, on using ++ operator on 'a', increments the value of 'a' by 1 and so java do simple addition 65 + 1 = ? You know! (The '(int) a' is type casting char to int hope you know abi!)� But what if I had used character �C� in the above, what will my output be?

Email Me: infomas6@gmail.com

  

13. What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

boolean var1 = true;

boolean var2 = false;

if(var1)

System.out.println (var1);

else

System.out.print (var2);

          }

     }


 a) 0

b) 1

c) true

d) false

Answer: c

Explanation: So you think if option c is correct then option b should also be correct because Boolean �true� is equivalent to 1 abi? Sorry but that is wrong! Boolean evaluate to either true or false! We only talk of it in terms of 1 or 0 considering them in binary terms or more appropriately algebraic sense. Remember if(var1) will return 'false' because var1 = false! So Java will skip to the else portion of the code to print out var2 (i.e. true)

 

14. What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

boolean var1 = true;

boolean var2 = false;

System.out.print((var1 & var2));

         }

     }


 a) 0

b) 1

c) true

d) false

 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.

15. What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

char var1 = 'A';

char var2 = 'a'

System.out.print ((int)var1 +  " " + (int)var2);

         }

     }


a) 162

b) 65 97

c) 67 95

d) 66 98

 Answer: b

Explanation: none

 16. What should be expression1 evaluate to in using ternary operator as in this line? expression1 ? expression2: expression3

a) Integer

b) Floating point numbers

c) Boolean

d) None of the mentioned

 Answer: c

Explanation: The controlling condition of ternary operator must evaluate to boolean.

 

17. What is the value stored in x in following lines of code?

int x, y, z;

x = 0;

y = 1;

x = y = z = 8;

a) 0

b) 1

c) 9

d) 8

 Answer: d

Explanation: None.

18. What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int x = 8

System.out.print (++x * 3 + " " + x);

         }

     }


 a) 24 8

b) 24 9

c) 27 8

d) 27 9

 Answer: d

Explanation: Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 then multiplied with 3 giving 27.

19. What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

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

 Answer: c

Explanation: None.

 20. What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

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.

 Answer: 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.

 21. An expression involving byte, int, and literal numbers is promoted to which of these?

a) int

b) long

c) byte

d) float

Answer: a

Explanation: An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any calculation is done.

22. Which data type value is returned by all transcendental math functions?

a) int

b) float

c) double

d) long

Answer: c

Explanation: None.

23. What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int g = 3;

System.out.print(++g * 8);

         }

     }


 a) 25

b) 24

c) 32

d) 33

 Answer: c

Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.

 24. What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

int array_variable[] = new int[10];

for(int j = 0; j<10; ++j)

{

array_variable[j] = j/2;

array_variable[j]++;

System.out.print(array_variable[j] + "");

i++

         }

     }

}


a) 0 2 4 6 8

b) 1 2 3 4 5

c) 0 1 2 3 4 5 6 7 8 9

d) 1 2 3 4 5 6 7 8 9 10

 Answer: b

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.

25.  What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int x;

x = 5;

{

int y = 6;

System.out.print(x + " " + y);

     }

}


a) 5 6 5 6

b) 5 6 5

c) Runtime error

d) Compilation error

Answer: d

Explanation: Second print statement doesn�t have access to y , scope y was limited to the block defined after initialization of x.  Exception in thread java.lang.Error: Unresolved compilation problem: y cannot be resolved to a variable

What will this code print?

int arr[] = new int [5];

System.out.print(arr);

a) 0

b) Value stored in arr[0].

c) 00000

d) Garbage value

Answer: d

Explanation: arr is an array variable, it is pointing to array if integers. Printing arr will print garbage value. It is not same as printing arr[0]

 Which of these is an incorrect Statement?

a) It is necessary to use new operator to initialize an array.

b) Array can be initialized using comma separated expressions surrounded by curly braces.

c) Array can be initialized when they are declared

d) None of the mentioned

 Answer: 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}

 Which of these is necessary to specify at time of array initialization?

a) Row

b) Column

c) Both Row and Column

d) None of the mentioned

 Answer: a

Explanation: None

What is the output of this program?

 class InfoMAS {

public static void main(String args[ ]){

int arr[] = new int[3][ ];

int arr[0] = new int[1];

int arr[1] = new int[2];

int arr[2] = new int[3];

int sum = 0 ;

for(int i = 0; i<3; i++)

for(int j = 0; j<i + 1; ++j)

arr[i][j] = j + 1;

for(int i = 0; i<3; i++)

for(int j = 0; j<i + 1; ++j)

sum+= arr[i][j];

system.out.print(sum);

    }

}

 a)  11

b) 10

c) 13

d) 14

 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.

 What is the output of this program?

 class InfoMAS {

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] + � �)

          }

     }

}

 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

Answer: d

Explanation: None.

 Which of these is not a bitwise operator?

a) &

b) &=

c) |=

d) <=

Answer: d

Explanation: <= is a relational operator.

 Which operator is used to invert all the digits in binary representation of a number?

a) ~

b) <<<

c) >>>

d) ^

 Answer: a

Explanation: Unary not operator ~ inverts all of the bits of its operand in binary representation.

On applying Left shift operator, <<, on an integer bits are lost once they are shifted past which position bit?

a) 1

b) 32

c) 33

d) 31

  Answer: a

Explanation: none

What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int var1 = 42;

int var2 = ~var1;

system.out.print(var1 + "" + var2);

          }

     }


 a) 42 42

b) 43 43

c) 42 -43

d) 42 43

 Answer: c

Explanation: Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i.e. 11010101 which is -43 in decimal.

 What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int x;

x = 10;

x = x >> 1;

System.out.println(x);

          }

     }


 a) 10

b) 5

c) 2

d) 20

Answer: b

Explanation: Right shift operator, >>, divides the value by 2. I don't know..., but I guess you need to watch out for this

 Which of these statements is correct?

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

Explanation: No two case constants in the same switch can have identical values.

 What is the output of this program?

 class InfoMAS {

public static void main(String args[ ]){

int var1 = 5;

int var2 = 6;

if((var2 = 1) == var1)

System.out.println(var2);

else

System.out.print (++var2);

          }

     }

 a) 1

b) 2

c) 3

d) 4

 Answer: b

Explanation: var2 is initialized to 1. So the conditional statement returns false because 1 is not equal to 5, and the else part gets executed.

 What is the output of this program?


class InfoMAS {

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

 What is the output of this program?

class InfoMAS {

public static void main(String args[ ]){

int x =2;

int y =0;

for(; y <10; ++i){

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

Answer: c

Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue statement, therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue statements of first if. Control comes to print statement only in cases when y is odd.

 What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

int x, y = 1

int 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.

 Answer: b

Explanation: Because the && operator first evaluate the left hand side (i.e. x! = 10) which is false, so the operator will not check the right hand side (i.e. x/0 ==0) so the division by zero will not take place and the program will run smoothly! You should know that && operator evaluate true iff all the operands or variables are true and once it encountered a false value during evaluation of expression it dump the evaluation of the remaining part to return false.

 What is the numerical range of a char in Java?

a) -128 to 127

b) 0 to 256

c) 0 to 32767

d) 0 to 65535

 Answer: d

Explanation: Char occupies 16-bit in memory, so it supports 216 i.e. from 0 to 65535

 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

 Answer: c

Explanation: Unicode defines fully international character set that can represent all found in all human languages. Its range is from 0 to 65536.

  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

 Answer: d

Explanation: First 0 to 127 character set in Unicode are same as those of ISO-LAIN-1 and ASCII.

 What is the output of this program?


class InfoMAS {

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

Explanation: by now you should be studying the codes yourself! It is very simple now! But if you still want an explanation no problem enroll in my java class!

 

Which of these have highest precedence?

a) ()

b) ++

c) *

d) >>

Answer: a

Explanation: Order of precedence is (highest to lowest) a -> b -> c -> d.

 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

Answer: a

Explanation: None.

 Which of these statements are incorrect?

a) Equal to operator has least precedence.

b) Brackets () have highest precedence.

c) Division operator, /, has higher precedence than multiplication operator.

d) Addition operator, +, and subtraction operator have equal precedence.

 Answer: c

Explanation: Division operator, /, has equal precedence as of multiplication operator. In expression involving multiplication and division evaluation of expression will begin from right side when no brackets are used.

 What is the output of this program?

class InfoMAS {

public static void main(String args[ ]){

int var1 = 5;

int var2 = 6;

int var3;

var3=++ var2 * var1 / var1 + var2

System.out.print (var3);

         }

     }

a) 10

b) 11

c) 12

d) 56

 Answer: 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.

 Which of these lines of code will give better performance?

1. a | 4 + c >> b & 7;

2. (a | ((( 4 * c ) >> b ) & 7 ))

a) 1 will give better performance as it has no parentheses.

b) 2 will give better performance as it has parentheses.

c) Both 1 & 2 will give equal performance.

d) Dependent on the computer system.

 Answer: c

Explanation: Parentheses do not degrade the performance of the program. Adding parentheses to reduce ambiguity does not negatively affect

 What is the range of data type short in Java?

a) -128 to 127

b) -32768 to 32767

c) -2147483648 to 2147483647

d) None of the mentioned

Answer: b

 What is the range of data type byte in Java?

a) -128 to 127

b) -32768 to 32767

c) -2147483648 to 2147483647

d) None of the mentioned

Answer: a

 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.

 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)(3) actually works, even though a cast is not necessary, because a long can store a byte.

 Which of these literals can be contained in a data type float variable?

a) 1.7e-308

b) 3.4e-038

c) 1.7e+308T

d) 3.4e-050

What is the output of this program?


class InfoMAS {

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

Explanation: None.

 What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

double a = 295.04;

int b = 300;

byte c = (byte)a ;

byte d = (byte)b ;

System.out.print(c + " " + d);

         }

     }


a) 38 43

b) 39 44

c) 295 300

d) 295.04 300

 Answer: b

Explanation: Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable. b contains 300 which is larger than byte range i.e. -128 to 127 hence d contains 300 modulo 256 i.e. 44.

  What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

double r, pi, a;

r = 9.8

pi = 3.14

a = pi * r * r

System.out.print(a);

         }

     }


 

a) 301.5656

b) 301

c) 301.56

d) 301.56560000

 Answer: a

Explanation: None.

 Which of these is data type long literal?

a) 0x99fffL

b) ABCDEFG

c) 0x99fffa

d) 99671246

 Answer: a

Explanation: Data type long literals are appended by an upper or lowercase l or L. 0x99fffL is hexadecimal long literal.

Which of these is returned by operators &, ?

a) Integer

b) Boolean

/FONT None.nbsp;

c) Character

d) Float

 Answer: c

Explanation: None.

 Literals in java must be preceded by which of these?

a) L

b) l

c) d

d) L and l

 

Answer: d

Explanation: Data type long literals are appended by an upper or lowercase L

 

Literal can be of which of these data types?

a) integer

b) float

c) boolean

d) all of the mentioned

 Answer: d

Explanation: None

 Which of these can not be used for a variable name in Java?

a) identifier

b) Keyword

c) Identifier & keyword

d) None of the mentioned

 Answer: b

Explanation: Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc.

 

What is the output of this program?


class InfoMAS {

public static void main(String args[ ]){

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

int d[] = a;

int sum = 0;

for(int j = 0; j<3; ++j)

sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);

System.out.print(sum);

         }

     }


 a) 38

b) 39

c) 40

d) 41

 Answer: c

Which of these is incorrect string literal?

a) "Hello World"

b) "Hello\nWorld"

c) "\"Hello World""

d) "Hello

world"

 Answer: d

Explanation: all string literals must begin and end in same line

 What is the output of this program?


 class InfoMAS {

public static void main(String args[ ]){

double a, b;

a = 3.0;

b = 4.0;

double c;

Math.sqrt(a * a+b * b);

System.out.print(c);

             }

}


 a) 5.0

b) 25.0

c) 7.0

d) Compilation Error

 Answer: a

Explanation: Variable c has been dynamically initialized to square root of a * a + b * b, during run time.

 

What is the output of relational operators?

a) Integer

b) Boolean

c) Characters

d) Double

 

Answer: b

Explanation: None

 Which of these is returned by greater than, <, and equal to, ==, operator?

a) Integers

b) Floating - point numbers

c) Boolean

d) None of the mentioned

 

Answer: c

Explanation: All relational operators return a boolean value i.e. true and false.

 

Which of the following operators can operate on a boolean variable?

1. &&

2. = =

3. ?:

4. +=

a) 3 & 2

b) 1 & 4

c) 1, 2 & 4

d) 1, 2 & 3

 

Answer: 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.

 Which of these operators can skip evaluating right hand operand?

a) !

b) |

c) &

d) &&

 Answer: d

Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.

 

Which of these statement is correct?

a) true and false are numeric values 1 and 0.

b) true and false are numeric values 0 and 1.

c) true is any non zero value and false is 0.

d) true and false are non numeric values.

 Answer: d

Explanation: true and false are keywords, they are non numeric values which do no relate to zero or non zero numbers. true and false are boolean values.

 

What is the output of this program?

class InfoMAS  {

public static void main(String args[ ]) {

int var1 = 5;

int var2 = 6;

System.out.print (var1 > var2);

    }

}

 a) 1

b) 0

c) true

d) false

 

Answer: d

Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.

 

What is the output of this program?

 class InfoMAS  {

public static void main(String args[ ]) {

boolean a = true;

Boolean b = false;

boolean c = a | b;

boolean d = a & b;

boolean e = d ? b :c;

System.out.print (d + " " + e);

    }

}

a) false false

b) true true

c) true false

d) false true

 Answer: d

Explanation: Operator | (i.e. OR) returns true if any one operand is true, thus c = true | false = true. Operator & (i.e. AND) returns a true if both of the operand is true thus d is false. The conditional operator (?) or ternary operator assigns left hand side of the colon (:) if condition is true and right hand side of the colon (:) if condition is false. d is false thus e = d ? b : c, assigns c to e , e contains true.

 

What is the output of this program?

class InfoMAS  {

public static void main(String args[ ]) {

int x = 3;

int y = ~3;

int z;

z = x > y ? x : y

System.out.print (z);

    }

}

 a) 0

b) 1

c) 3

d) -4 

Answer: c

Explanation: None.

 

Which of these access specifiers must be used for main()

a) private

b) public

c) protected

d) None of the mentioned

 

Answer: b

Explanation: main() method must be specified public as it called by Java run time system, outside of the program. If no access specifier is used then by default member is public within its own package & cannot be accessed by Java run time system.

 

 Which of these is used to access member of class before object of that class is created?

a) public

b) private

c) static

d) protected

 

Answer: c

Explanation: None.

 

Which of these is used as default for a member of a class if no access specifier is used for it?

a) private

b) public

c) public, within its own package

d) protected

 Answer: a

Explanation: When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

 What is the process by which we can control what parts of a program can access the members of a class?

a) Polymorphism

b) Abstraction

c) Encapsulation

d) Recursion 

Answer: c

Explanation: None.

 Which of the following statements are incorrect?

a) public members of class can be accessed by any code in the program.

b) private members of class can only be accessed by other members of the class.

c) private members of class can be inherited by a sub class, and become protected members in sub class.

d) protected members of a class can be inherited by a sub class, and become private members of the subclass.

Answer: c

Explanation: private members of a class can not be inherited by a sub class.

 

Which of these access specifiers must be used for class so that it can be inherited by another sub class?

a) public

b) private

c) protected

d) None of the mentioned 

Answer: a

Explanation: none

 

What is stored in the object obj in following lines of code?

box obj;

a) Memory address of allocated memory of object.

b) NULL

c) Any arbitrary pointer

d) Garbage

 

Answer: b

Explanation: Memory is allocated to an object using new operator. Box obj; just declares a reference to object, no memory is allocated to it hence it points to NULL.

 Which of these keywords is used to make a class?

a) class

b) struct

c) int

d) None of the mentioned

 

Answer: a

Explanation: None.

 Which of the following is a declaration of an object of class Box?

a) Box obj = new Box();

b) Box obj = new Box;

c) obj = new Box();

d) new Box obj; 

Answer: a

Explanation: None.

 Which of these operators is used to allocate memory for an object?

a) malloc

b) alloc

c) new

d) give 

Answer: c

Explanation: Operator new dynamically allocates memory for an object and returns a reference to it. This reference is address in memory of the object allocated by new.

 Which of these statements is incorrect?

a) Every class must contain a main() method

b) Applets do not require a main() method at all.

c) There can be only one main() method in a program.

d) main() method must be made public.

 Answer: a

Explanation: Every class does not need to have a main() method, there can be only one main() method which is made public.

 

What is the output of this program?

class  InfoMAS  {

public static void main(String args[ ]) {

int x = 9;

if (x ==9) {

int x = 8;

System.out.println (x);

        }

    }

}

 

a) 9

b) 8

c) Compilation error

d) Runtime error

 Answer: c

Explanation: Two variables with the same name can't be created in a class. At first x was declared as integer storing 9 (int x = 9) and then x was declared again as integer storing 8 (int x = 8). These declarations just get java compiler confused and boom! It throws an error during compilation

 Which of the following Statements is correct?

a) Public method is accessible to all other classes in the hierarchy

b) Public method is accessible only to subclasses of its parent class

c) Public method can only be called by object of its class.

d) Public method can be accessed by calling object of the public class.

 Answer: a

Explanation: None.

 Which of these keywords is used to define packages in Java?

a) pkg

b) Pkg

c) package

d) Package

 

Answer: c

Explanation: None.

 Which of these is a mechanism for naming and visibility control of a class and its content?

a) Object

b) Packages

c) Interfaces

d) None of the Mentioned

 

Answer: b

Explanation: Packages are both naming and visibility control mechanism. We can define a class inside a package which is not accessible by code outside the package.

 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

 

Answer: d

Explanation: Either we can use public, protected or we can name the class without any specifier.

 

Which of these access specifiers can be used for a class so that its members can be accessed by a different class in the different package?

a) Public

b) Protected

c) Private

d) No Modifier

 

Answer: a

Explanation: None.

 Which of the following is correct way of importing an entire package pkg?

a) import pkg.

b) Import pkg.

c) import pkg.*

d) Import pkg.*

 Answer: c

Explanation: Operator * is used to import the entire package.

 

Which of the following is incorrect statement about packages?

a) Package defines a namespace in which classes are stored.

b) A package can contain other package within it.

c) Java uses system directories to store packages.

d) A package can be renamed without renaming the directory in which the classes are stored.

 Answer: d

Explanation: A package can be renamed only after renaming the directory in which the classes are stored.

 Which of the following package stores all the standard java classes?

a) lang

b) java

c) util

d) java.packages

 Answer: b

Explanation: None.

 

What is the output of this program?

 package pkg;

class  InfoMAS  {

public static void main(String args[ ]) {

StringBuffer cpt221 = new StringBuffer(�Hello�);

cpt221.setCharAt(1, x);

System.out.println (cpt221);

        }

    }

a) xello

b) xxxxx

c) Hxllo

d) Hexlo

 

Answer: c

Explanation: Observe the heading and see that a certain package (pkg) was included in this program. So do not wonder what-the-hell! is StringBuffer!. We use this to create a string object call cpt221 (thanks to the inclusion of the package pkg) and then set the string "Hello". Then we used the string method cpt221.setCharAt(1, x). Interesting! What this line does is; replace the character at position 1 in "Hello" with the character "x" (i.e. this replace "e" with "x" in "Hello". I hope I am not confusing you anyway?!

 What is the output of this program?

package pkg;

class  InfoMAS  {

public static void main(String args[ ]) {

StringBuffer cpt221 = new StringBuffer(�Hello World�);

cpt221.insert(6, �Good�);

System.out.println (cpt221);

        }

//InfoMAS.class file is not in directory pkg.

    }

 a) HelloGoodWorld

b) HellGoodoWorld

c) Compilation error

d) Runtime error

 Answer: d

Explanation: The last line, the comment "InfoMAS.class file is not in directory pkg" explain the answer to this question  Since InfoMAS.class file is not in the directory pkg in which class InfoMAS is defined, program will not be able to run.

 Which of these of class String is used to compare two String objects for their equality?

a) equals()

b) Equals()

c) isequal()

d) Isequal()

Answer: a

Explanation: equals() is a string object for comparing equality of strings. Its return value is Boolean (i.e. True/False)

 Which of these compare a specific region inside a string with another specific region in another string?

a) regionMatch()

b) match()

c) RegionMatches()

d) regionMatches()

 Answer: d

Explanation: So why is option c not correct but d since they are same spelling? Java is case sensitive. Java has a general convention in naming (especially objects and method) known as the Carmel naming convention. The rule is this; if I am going to name a method or object using the words John and Doe, then if the word John come first in the naming before Doe then begin the first latter of John with small letter and the first letters of the other words with Capital letters. So in this case I have johnDoe. If Doe is to come first in the naming then I have doeJohn. For the three words Habiba, Juliet and Jefferson put together for naming in Java we can have; habibaJulietJefferson or julietHabibaJerfferson. For region and matches we have regionMatches. Most object and method follows this naming convention in Java although it is allow using whatever convention you want.

 Which of these methods of class String is used to check weather a given object starts with a particular string literal?

a) startsWith()

b) endsWith()

c) Starts()

d) ends()

Answer: a

Explanation: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is specialized form of method regionMatches()

 

What is the value returned by function compareTo() if the invoking string is less than the string compared?

a) zero

b) value less than zero

c) value greater than zero

d) None of the mentioned

 Answer: b

Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared

 Which of these data type value is returned by equals() method of String class?

a) char

b) int

c) Boolean

d) All of the mentioned

 Answer: c

Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal

 What is the output of this program?

class  InfoMAS  {

public static void main(String args[ ]) {

String c = "Hello I love java";

boolean var;

var = c.startWith("hello");

System.out.println (var);

        }

    }

a) true

b) false

c) 0

d) 1

Answer: b

Explanation: startsWith() method is case sensitive "hello" and "Hello" are treated differently, hence false is stored in var.

 What is the output of this program?

class  InfoMAS  {

public static void main(String args[ ]) {

char ch

ch = �hello�.charAt(1)

System.out.println (ch);

        }

    }

a) h

b) e

c) l

d) o

Answer: b

Explanation: "hello" is a String literal, method charAt() returns the character specified at the index position Character at index position 1 is e of hello, hence ch contains e.

 What is the return type of a method that does not returns any?

a) int

b) float

c) void

d) double

 

Answer: c

Explanation: Return type of any method must be made void if it is not returning any value.

 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

 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.

Which of the following is a method having same name as that of it class?

a) finalize

b) delete

c) class

d) constructor

 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

 Which method can be defined only once in a program?

a) main method

b) finalize method

c) static method

d) private method

 

Answer: a

Explanation: main() method can be defined only once in a program. Program execution begins from the main() method by java run time system

 Which of these statements is incorrect?

a) All object of a class are allotted memory for the all the variables defined in the class.

b) If a function is defined public it can be accessed by object of other class by inheritation.

c) main() method must be made public.

d) All object of a class are allotted memory for the methods defined the class.

 Answer: d

Explanation: All object of class share a single copy of methods defined in a class, Methods are allotted memory only once. All the objects of the class have access to methods of that class are allotted memory only for the variables not for the methods

What is process of defining two or more methods within same class that have same name but different parameters declaration?

a) method overloading

b) method overriding

c) method hiding

d) None of the mentioned

Answer: a

Explanation: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.

 Which of these can be overloaded?

a) Methods

b) Constructors

c) All of the mentioned

d) None of the mentioned

 Answer: c

Explanation: None

 Which of these is correct about passing an argument by call-by-value process?

a) Copy of argument is made into the formal parameter of the subroutine.

b) Reference to original argument is passed to formal parameter of the subroutine.

c) Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument.

d) Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument

 Answer: a

Explanation: When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

 What is the process of defining a method in terms of itself, that is a method that calls itself?

a) Polymorphism

b) Abstraction

c) Encapsulation

d) Recursion

 

Answer: d

Explanation: None

 

Which of the following statements are incorrect?

a) Default constructor is called at the time of declaration of the object if a constructor has not been defined.

b) Constructor can be parameterized.

c) finalize() method is called when a object goes out of scope and is no longer needed.

d) finalize() method must be declared protected

 

Answer: c

Explanation: finalize() method is called just prior to garbage collection. It is not called when object goes out of scope.

 String in Java is a?

a) class

b) object

c) variable

d) character array

 Answer: a

Explanation: String is a class

 Which of these of String class is used to obtain character at specified index?

a) char()

b) Charat()

c) charat()

d) charAt()

 Answer: d

Explanation: None.

 Which of these keywords is used to refer to member of base class from a sub class?

a) upper

b) super

c) this

d) None of the mentioned

 Answer: b

Explanation: Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword

 

Which of these methods of String class can be used to test two strings for equality?

a) isequal()

b) isequals()

c) equal()

d) equals()

 Answer: d

Explanation: None.

 

Which of the following statements are incorrect?

a) String is a class.

b) Strings in java are mutable.

c) Every string is an object of class String.

d) Java defines a peer class of String, called StringBuffer, which allows string to be altered.

 

Answer: b

Explanation: Strings in Java are immutable that is they can not be modified.

 

Which of the following can be operands of arithmetic operators?

a) Numeric

b) Boolean

c) Characters

d) Both Boolean & Characters

 

Answer: d

Explanation: operand Arithmetic operators can be any of arithmetic operators?

 a) numeric

b) Boolean

c) character

d) Both Boolean and character

 

With x = 0, which of the following are legal way 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 

Answer: c

Explanation: none

 

The decrement operator (�) decreases value of variables by what number

a) 1

b) 2

c) 3

d) 4

Answer:  1

 Explanation

 What is the output of this program?

class  InfoMAS  {

public static void main(String args[ ]) {

double var1 = 1 + 5;

double var2 = var1/4;

int var3 = 1 + 5;

int var4 = var3/4;

System.out.print(var2 + " " + var4);

        }

    }

 a) 1 1

b) 0 1

c) 1.5 1

d) 1.5 1.0

 Answer: c

 Explanation: none

 What is the output of this program?

class  InfoMAS  {

public static void main(String args[ ]) {

double a = 25.64;

int b = 25;

a = a%10;

b = b%10;

System.out.print(a + " " + b);

        }

    }

a) 5.640000000000001 5

b) 5.640000000000001 5.0

c) 5 5

d) 5 5.640000000000001

 Answer: a

Explanation: Modulus operator returns the remainder of a division. So a = a%10 i.e. 25.64 % 10 = 5.640000000000001 and b = 25 % 10 = 5 and in the last line we printed out a and b concatenating and separating them with a white space through the + operator.

 What is the output of this program?

class  InfoMAS  {

public static void main(String args[ ]) {

int x, y;

x = 10;

x++;

--x;

y =  x++;

System.out.print(x + " " + y);

        }

    }

a) 11 11

b) 10  10

c) 11 10

d) 10 11

 Answer: c

Explanation: none

 What is the output of this program?

class  InfoMAS  {

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.print(a + " " + b + " " + c);

        }

    }

a) 3 2 4

b) 3 2 3

c) 2 3 4

d) 3 4 4

 

Answer: d

Explanation: a, b, c, d are variables with a and b initialized as 1 and 2 (i.e. a = 1 and b = 2) next we increment b by 1 and assign it to c (i.e. c = ++b) so now b has a new value of 3 as well as c. Next we assign a to d and then increment a by 1 (d = a++). So now d = 1 and a = 2 (Note that the assignment operation (d = a++) assigned the variable a first to d before it then increment the variable a by 1. This is different from the assignment for example say (d = ++a) in which the variable a is increment by 1 before assigning to d). Next we increment c by 1 and so c is now 4 then we increment b by 1 and so b is now 4, finally we increment a by one and a is now 3. Concatenating a b c therefore we have 3 4 4

 

 Which of these packages contain classes and interfaces use for input and output operations of a program?

a) java.util

b) java.lang

c) java.io

d) All of the mentioned

 Answer: c

Explanation: java.io provides support for input and output operations.

 

The keyword javac stands for what?

a) java compile

b) java compiler

c) java compilation

d) java console

 Answer b:

Which of these class is not a member class of java.io package?

a) String

b) StringReader

c) Writer

d) File

 

Answer: a

Explanation: None.

 

Which of these interface is not a member of java.io package?

a) DataInput

b) ObjectInput

c) ObjectFilter

d) FileFilter

 

Answer c:

Explanation: None

 

Which of these class is not related to input and output stream in terms of functioning?

a) File

b) Writer

c) InputStream

d) Reader

 

Which of these is specified by a File object?

a) a file in disk

b) directory path

c) directory in disk

d) None of the mentioned

Answer: c

Explanation: None.

 

Which of these is method for testing whether the specified element is a file or a directory?

a) IsFile()

b) isFile()

c) Isfile()

d) isfile()

Answer: b

Explanation: none

 


Powered by InfoMAS

Do you have any complain or suggestion for InfoMAS?

email Us: infomas6@gmail.com

Or use any of our comment box

Post at us on facebook: FUTMinna informant

InfoMAS


nbsp;