• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

questions from java prepare

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Which of the following are legal Java programs. Select all the correct answer.
A.// The comments come before the package
package pkg;
import java.awt.*;
class C{}
B.package pkg;
import java.awt.*;
class C{}
C.package pkg1;
package pkg2;
import java.awt.*;
class C{}
D.package pkg;
import java.awt.*;
E.import java.awt.*;
class C{}
F.
the correct answers are a,b,d,e.
how d can be a correct answer.in the explanation they have given that it is an empty file.what is the meaning of that.And how do you save the file at d.
2.Which of the following statements are correct. Select all correct answers.
A.A Java program must have a package statement.
B.A package statement if present must be the first statement of the program (barring any comments).
C.If a Java program defines both a package and import statement, then the import statement must come before the package statement.
D.An empty file is a valid source file.
E.A Java file without any class or interface definitions can also be compiled.
F.If an import statement is present, it must appear before any class or interface definitions
The correct answers are
b, d, e, f.
my question is they have given option e as correct.How do a java
file without any class or interface defination can be compiled.

3.What would be the results of compiling and running the following class. Select the one correct answer.

class test {
public static void main() {
System.out.println("test");
}
}

A.The program does not compile as there is no main method defined.
B.The program compiles and runs generating an output of "test"
C.The program compiles and runs but does not generate any output.
D.The program compiles but does not run.
The correct answers is d
how does the program cannot run.the output is test.

4.Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
A.arr.length
B.arr.length - 1
C.arr.size
D.arr.size - 1
E.arr.length()
F.arr.length() - 1
the answer is A.
length() is a method. so how can A is the right answer.E is the right answer.
5. Which of the following are legal declaration and definition of a method. Select all correct answers.
A.void method() {};
B.void method(void) {};
C.method() {};
D.method(void) {};
E.void method {};
the correct answer given is only E.
while a is also the right answer.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I'll try to answer these the best I can:
I'll start with the last one and work my way up.
5. A. is the only correct answer. E. is missing the parentheses after the method name. I believe this is a typo.
4. A. is again the right answer. If you consult the Jdk documentation you'll find that the only way to find the length of an array is with "<arrayname>.length". So for an array named arr, arr.length is the way to find out how big it is.
3. will compile fine because you define a method named "main" correctly but every program needs a public static void main(String args[]){} to compile and run. You've overidden this method from superclass Object.
2. Try it yourself, and you'll see that e is correct as well. This is often the best way to answer you're own questions. Write a program and see if it'll work. It's also great practice.
1. this is also correct. Try it yourself. put a package statement in followed by a legal import statement. then name the file correctly such as blue.java and compile it. The file will compile, but it won't give off a file named blue.class since there is no such class defined.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am giving the answer of the 4th one.
u r absolutely right when u say that .length()is the method to find the length-but the length of what??
it is used to find the length of a String.
to find the length of an array,we always use .length
w/out any parentheses.
 
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic