Java Programming Questions and Answers Set 29

Java Programming OOPs

Questions 281 to 290



281.
What is the output of the following code?
class Arithmetic {
      public static void main (string args[]) {
int x=17, y=5;
          System.out.println(“x =” +x);
          System.out.println(“y =” +y);
          System.out.println(“x+y=” +(x+y));
          System.out.println(“x-y=” +(x-y));
          System.out.println(“x*y=” +(x*y));
          System.out.println(“x/y=” +(x/y));
          System.out.println(“x%y=” +(x%y));
   }
}
(a)
5, 17, 12, 85, 3, 2
(b)
17, 5, 22, 12, 85, 3, 2
(c)
17, 5, 20, 21, 85, 3, 2
(d)
2, 3, 85, 12, 22, 5, 17
(e)
17, 5, 12, 22, 85, 2, 3.
282.
What is the output of the following code?
class SpecialArithmetic {
    public static void main (string args[]) {
          int x=17,a,b;
         a=x++;b=++x;
         System.out.println(“x=” + x +“a=” +a);
          System.out.println(“x=” + x + “b=” +b);
          a=x--;b=--x;
          System.out.println(“x=” + x + “a=” +a);
          System.out.println(“x=” + x + “b=” +b);
          }
}
(a)
x=18 a=17
x=19 b=19
x=18 a=19
x=17 b=17
(b)
x=19 a=17
x=18 b=19
x=18 a=19
x=17 b=17
(c)
x=19 a=17
x=19 b=19
x=18 a=20
x=17 b=17
(d)
x=19 a=17
x=19 b=19
x=18 a=19
x=17 b=17
(e)
x=19 a=18
x=19 b=19
x=18 a=19
x=17 b=17.
283.
What is the output of the following code?
class Bitwise {
  public static void main (string args[]) {
            int x=5, y=6;
            System.out.println(“x&y=” +(x &y));
            System.out.println(“x|y=” +(x|y));
            System.out.println(“x^y=” +(x^y));
            }
}
(a)
x&y=5
x|y=7
x^y=3
(b)
x&y=4
x|y=7
x^y=3
(c)
x&y=6
x|y=7
x^y=3
(d)
x&y=4
x|y=8
x^y=3
(e)
x&y=4
x|y=8
x^y=4.
284.
What is the output of the following code?
class Shift {
       public static void main (string args[ ]) {
            int x=7;
            System.out.println(“x>>>1=” +(x>>>1));
       }
}
(a)
x>>>1=3
(b)
x>>>1=6
(c)
x>>>1=4
(d)
x>>>1=5
(e)
x>>>1=2.
285.
class Relational{
         public static void main (string args[]) {
               int x=7, y=11, z=11;
               System.out.println(“y<=z =” +(y<=z));
 }
}
(a)
y<=z = 0
(b)
y<=z = 1
(c)
y<=z = true
(d)
y<=z = false
(e)
y<z = true.
286.
char chars[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’};
            String s = new String(chars, 2, 3);
            System.out.println (s);     
What is the output of the  above code?
(a)
abc
(b)
bcd
(c)
cde
(d)
deb
(e)
acd.
287.
String Firstname[]= new  String[5]
String firstName [ ] = { “Kamal”, “Amal” , “Nimal
                                                            “Saman”, “Sunil” } ;
What is value of firstName[5]?
(a)
Kamal
(b)
Amal
(c)
Saman
(d)
Sunil
(e)
Throws an exception.
288.
int twoDMatrix [ ][ ] = new int [4][6];
int twoDMatrix [ ][ ] = { {5,1,2,8,6,9}
                {8,4,1,6,2,1}
                 {3,9,2,5,8,9}
                 {1,7,3,6,4,3} };
What is the output of the following statement twoDMatrix[0].length?
(a)
3
(b)
4
(c)
5
(d)
6
(e)
7.
289.
Which operator is used for concatenation in java?
(a)
-
(b)
Dot
(c)
*
(d)
%
(e)
+.
290.
class Point {
             int x,y;
             Point (int x, int y) { 
                    this.x = x;
                    this.y = y;   
             }
             public String toString() {
                     return “Point[“ +x+ “,” +y+ “]”;
             } }
   class toStringDemo {
          public static void main (String args[]) {
                Point p = new Point (10,20);
                System.out.println(“p=“ +p);}
             }
What is the output of the following code?
(a)
p = Point[10,20]
(b)
p = Point[20,20]
(c)
p = Point[20,10]
(d)
p = Point[11,20]
(e)
p = Point[10,21].





Answers




281.
Answer : (b)
Reason:  B is the right choice
282.
Answer : (d)
Reason:  According to the rule of increment operator the choice is D.
283.
Answer : (b)
Reason:  According to the rule of bitwise operator the choice is B
284.
Answer : (a)
Reason:  According to the rule of the Right Shift operator the following expression is evaluated to  3
285.
Answer : (c)
Reason:  According to the rule of the Relational operator precedence c is the apt choice.
286.
Answer : (c)
Reason:  String(chars,2,3) means starting at the position 2 extract 3 characters.here indexing start from o so first character is c and remaining are d and e.
287.
Answer : (e)
Reason:  As the array is out of bound
288.
Answer : (d)
Reason:  The given matrix is of 4 rows and each row containing 6 elements.
289.
Answer : (e)
Reason:  + operator is used for concatenation in java
290.
Answer : (a)
Reason:  Shows a class which overrides toString   to show the values of its instance variables.

<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


1 comment :