Java Programming Questions and Answers Set 22

Java Programming OOPs

Questions 211 to 220



211.
Which of the following is/are valid comment(s) in java?
(a)
/** comment */
(b)
/* comment */
(c)
/* comment
(d)
// comment
(e)
Both (a) and (b).
212.
What is the correct signature of the main method?
(a)
public static  main(String[] args)
(b)
public static void main(String[] args)
(c)
public static void main(String[] )
(d)
public static void main(String  args)
(e)
public void main(String[] args).
213.
Which of the following is not a valid statement?
(a)
Instance methods can access instance variables and instance methods directly
(b)
Instance methods can access class variables and class methods directly
(c)
Class methods can access class variables and class methods directly
(d)
Class methods can access instance variables or instance methods directly
(e)
Class methods cannot use ‘this’ keyword as there is no instance for this to refer to.
214.
What is the correct order of defining a Class?
1.    The name of the class's parent (super class), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
2.    The class name, with the initial letter capitalized by convention.
3.    Modifiers such as public, private, and a number of others that you will encounter later.
4.    The class body, surrounded by braces, {}.
5.    A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
(a)
3,1,2,5,4
(b)
1,2,3,5,4
(c)
3,2,1,5,4
(d)
5,1,2,3,4
(e)
5,4,3,2,1.
215.
public class PassPrimitiveByValue {
public static void main(String[] args) {     
int x = 3;     
//invoke passMethod() with x as argument
passMethod(x);
// print x to see if its value has changed
System.out.println("After invoking passMethod, x = " + x);     
}
// change parameter in passMethod()
public static void passMethod(int p) {
p = 10;
}
}
What is the output of the above program?
(a)
10
(b)
3
(c)
garbage value
(d)
4
(e)
10.3.
216.
What is the output of the following code?
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
IdentifyMyParts.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("IdentifyMyParts.x = " + a.x);
System.out.println("b.x = " + b.x);
(a)
a.y = 5
b.y = 6
a.x = 2
b.x = 2
(b)
a.y = 5
b.y = 5
a.x = 2
b.x = 2
(c)
a.y = 5
b.y = 6
a.x = 3
b.x = 2
(d)
a.y = 5
b.y = 6
a.x = 2
b.x = 3
(e)
a.y = 6
b.y = 6
a.x = 2
b.x = 2.
217.
Which of the following is legal valid variable name?
(a)
1num
(b)
#twenty
(c)
@one
(d)
timeOfDay
(e)
temp val.
218.
What is the bit size of int in java?
(a)
8
(b)
16
(c)
32
(d)
64
(e)
128.
219.
Which one of these is a valid constructor declaration for the class named student?
(a)
Student(student  b) { }
(b)
Student  Student() { }
(c)
private final  Student[] { }
(d)
void Student() { }
(e)
abstract student() { }.
220.
Which access specifiers should be implemented in a class for all interface methods?
I.     Private.
II.     Public.
III.    Protected.
(a)
Only (I) above
(b)
Only (II) above
(c)
Only (III) above
(d)
Both (I) and (III) above
(e)
All (I), (II) and (III) above.


Answers


211.
Answer : (c)
Reason:  The Java programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */.
// text
The compiler ignores everything from // to the end of the line.
212.
Answer : (b)
Reason:  The correct signature is public static void main(String[] args)
213.
Answer : (d)
Reason:  Class methods cannot access instance variables or instance methods directly—they must use an object reference.
214.
Answer : (c)
Reason:  class declarations can include these components, in order:
1.    Modifiers such as public, private, and a number of others that you will encounter later.
2.    The class name, with the initial letter capitalized by convention.
3.    The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
4.    A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
The class body, surrounded by braces, {}.
215.
Answer : (b)
Reason:  Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.So the output is 3 only.
216.
Answer : (a)
Reason:  Remaining all are contradictory.
217.
Answer : (d)
Reason:  Can only Start with a Letter, Underscore (_) , or Dollar Sign ($).After the first letter Name can include any letter or Number but cannot include Symbols such as %, @, *  and so on.
218.
Answer : (c)
Reason:  The bit size of int is 32.
219.
Answer : (a)
Reason:  A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
220.
Answer : (b)
Reason:  Interfaces are abstraction of functionality that can be implemented in more than one way when required. The abstraction must be made available as public for the use by other parts of the program.


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


2 comments :

  1. Q 211. Which of the following is/are valid comment(s) in java?

    It should be "Which of the following is/are INVALID comment(s) in java?"

    ReplyDelete
  2. This is a great article.I am y much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.

    java examples

    ReplyDelete