1. Java 7 NIIT
Page 1 of 7
JAVA7 Questions
1) Whichisan invalidstatement.
a. longa=123_456_ _789L;
b. longb=0XCAFE_BABE;
c. longc=123_456_789L;
d. longd=567.89L;
2) Switchstatementinjavawill notaccept_______ datatype.
a. String
b. Long
c. Enum
d. Char
3) Sam iss/w developerwhoisworkingforXYZtechnosolutions.Forvariousreasonshe needtoextend
a class fromanotherclasswhere he needall the initial valuesof the variablesinthe existingclass.How
can he getit.
a. Do nothingextrajustextendnew classfromexistingclass
b. Write hisownconstructor
c. Thisis notsupportedbyJava
d. Use Collectionsandgenerics
4) Abilitytorefertoan objectusingeitherit’sactual formor parentformiscalled
a. Inheritance
b. Polymorphism
c. Instantiation
d. Generalization
5) A. Defaultmemberscanbe accessedinsame andsub classesinsame as well asdifferentpackages.
B. Publicvariablescanbe accessedinsame and subclassesinsame but notdifferentpackages.
a. A istrue B istrue
b. A istrue B isfalse
c. A isfalse B is true
d. A isfalse B isfalse
6) Nonprivate fieldsof superclasscanbe declaredinsubclassandthe fieldscanbe hiddeninsubclass
a. True-Itsdue tothe inheritance feature supportedbyjava
b. True-Itscalledasfieldshadowing
c. True- Its calledasdata hiding(AbstractionandEncapsulation)
d. False-Notsupportedbyjava
7) A. Abstractclass cannotbe instantiated
B. A classextendinganabstractclassmay or may not override the methodsof the abstractclass.
2. Java 7 NIIT
Page 2 of 7
a. A istrue B istrue
b. A istrue B isfalse
c. A isfalse B istrue
d. A isfalse B isfalse
8) What is the outputof the followingcode snippet.
class static_out {
static int x;
static int y;
void add(int a , int b){
x = a + b;
y = x + b;
}
}
class static_use {
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
9) Find the output
1. class PersonalLoan{
2. public final String getName(){
3. return "personal loan";
4. }
5. }
6. class CheapPersonalLoan extends PersonalLoan{
7. @Override
8. public final String getName(){
9. return "cheap personal loan";
10. }
11. }
12.
a) Error at line2
b) Error at line6
c) Error at line8
d) Cheap personal loan
3. Java 7 NIIT
Page 3 of 7
10)
13. enum Color{
14. black,white,green,yellow;
15. }
16. public class EnumTest {
17. public static void main(String[] args){
i. for(Color bar: Color.values())
ii. System.out.println(bar.ordinal());
18. }
19. }
What will output when you compile and run the above java code?
(a)1 2 3 4
(b)0 1 2 3
(c)a b c d
(d)97 98 99 100
11)
1. import java.io.*;
2. class serialization {
3. public static void main(String[] args) {
4. try {
5. Myclass object1 = new Myclass("Hello", -7, 2.1e10);
6. FileOutputStream fos = new FileOutputStream("serial");
7. ObjectOutputStream oos = new ObjectOutputStream(fos);
8. oos.writeObject(object1);
9. oos.flush();
10. oos.close();
11. }
12. catch(Exception e) {
13. System.out.println("Serialization" + e);
14. System.exit(0);
15. }
16. try {
17. Myclass object2;
18. FileInputStream fis = new FileInputStream("serial");
19. ObjectInputStream ois = new ObjectInputStream(fis);
20. object2 = (Myclass)ois.readObject();
21. ois.close();
22. System.out.println(object2);
23. }
24. catch (Exception e) {
25. System.out.print("deserialization" + e);
26. System.exit(0);
27. }
28. }
29. }
30. class Myclass implements Serializable {
31. String s;
32. int i;
33. double d;
34. Myclass (String s, int i, double d){
4. Java 7 NIIT
Page 4 of 7
35. this.d = d;
36. this.i = i;
37. this.s = s;
38. }
39. }
40. @Override
41. public String toString() {
42. return "String tt" + gets() + "n"
43. + "int tt" + geti() + "n"
44. + "double " + getd();
45. }
a) String=Hello;int=-7;double=2.1E10
b) Hello;-7; 2.1E10
c) s; i; 2.1E10
d) Serialization
12)
1. public class MaximumTest
2. {
3. public static <T extends Comparable<T>> T maximum(T x, T y, T z)
4. {
5. T max = x
6. if ( y.compareTo( max ) > 0 ){
7. max = y; }
8. if ( z.compareTo( max ) > 0 ){
9. max = z;
10. }
11. return max
12. }
13. public static void main( String args[] )
14. {
15. System.out.printf( "Max of %d, %d and %d is %dnn",
16. 3, 4, 5, maximum( 3, 4, 5 ) );
17. System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1fnn",
18. 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
19. System.out.printf( "Max of %s, %s and %s is %sn","pear",
20. "apple", "orange", maximum( "pear", "apple", "orange" ) );
21. }
22. }
a) Maximum of 3, 4 and 5 is 5
Maximum of 6.6, 8.8 and 7.7 is 8.8
Maximum of pear, apple and orange is pear
b) Error at line3
c) Error at line 6,8
d) Compiles successfullybutnooutput
13) A.StringBuildersare usedinsteadof Stringincase of frequentmodificationsrequiredandcanbe
usedinsynchronizedblocks/codes.
5. Java 7 NIIT
Page 5 of 7
B. StringBuffersare similartoStringBuildersexcepttheycannotbe usedinsynchronized
blocks/codes.
a. A istrue B istrue
b. A istrue B isfalse
c. A isfalse B istrue
d. A isfalse B isfalse
14) A.split() canparse stringusingmultiple delimiters.
B. StringTokenizercanparse Stringusingmultiple delimiters.
a. A istrue B istrue
b. A istrue B isfalse
c. A isfalse B istrue
d. A isfalse B isfalse
15) Findoutput.
1. public class Rex
2. {
3. public static void main( String args[] ){
4. String line = "This order was placed for QT3000! OK?";
5. String pattern = "(.*)(QT)(d+)(.*)";
6. Pattern r = Pattern.compile(pattern);
7. Matcher m = r.matcher(line);
8. if (m.find( )) {
9. System.out.println("Found value: " + m.group(0) );
10. System.out.println("Found value: " + m.group(1) );
11. System.out.println("Found value: " + m.group(2) );
12. System.out.println("Found value: " + m.group(3) );
13. System.out.println("Found value: " + m.group(4) );
14. } else {
15. System.out.println("NO MATCH");} }}
a) Will notcompile,erroratline 4,5
b) Foundvalue:Thisorderwas placedforQT3000! OK?
Foundvalue:Thisorderwas placedfor
Foundvalue:QT
Foundvalue:3000
Foundvalue:! OK?
c) Compilessuccessfullybutnooutput
d) Foundvalue:Thisorderwas placedfor
Foundvalue:QT
Foundvalue:3000
Foundvalue:! OK?
Foundvalue:
16) Findthe wrongchoice
6. Java 7 NIIT
Page 6 of 7
a) While usingatry withresource AutoCloseble interface neednotbe implemented,itsincluded
by defaut.
b) In a try withresource statementmultiple resourcescanbe opened.
c) Resourcesare closedinopposite orderof theirusage intrywithresource
d) Try withresourcesare mainlytoavoidlengthyfinallystatements.
17) Findthe rightchoice
a) In a try withresource statementif anexceptionoccursatthe time of resource creationthe
exceptionhandlerisexecuted.
b) In a try withresource statementif anexceptionoccursatthe time of closingthe resource the
program stopsabruptly.
c) In a try withresource statementif anexceptionoccursat the time of resource creationthe try
blockisexecuted.
d) In a try withresource statementif anexceptionoccursatthe time of closingthe resource the
exceptionisexecuted.
18) Findthe outputof the code snippetandassume there ismyf.txtfile inthe pathpppandfile myff.txt
isnot presentinthe path specifiedinpz.
1. Path ppp=Paths.get("D:Tempfoobarexamplecarmyf.txt");
2. Path pp = ppp.subpath (1, 5);
3. pz=Paths.get("D:Tempfoobarexamplecarmyff.txt");
4. System.out.format("Path %s exists: %b%n", p5,Files.exists(p5,
LinkOption.NOFOLLOW_LINKS));
5. Files.copy(ppp,pz,StandardCopyOption.REPLACE_EXISTING);
6. System.out.println("copied");
a)Error at line1
b)error at line4
c)error at line5
d)will copy the contents of myf.txt to myff.txt and will display the
output copied.
19) While programmingwiththreadsyourthreadneedtoreturnsome value.Forthatyouwill
a) ExtendThreadandoverride run()
b) ImplementRunnable andoverriderun()
c) ImplementCallable andoverriderun()
d) Not supportedbyjava
20) To submita workby a threadone shouldmake use of
a) ThreadClass
b) Runnable Inteface
c) ExecutorServiceClass
d) ResultSetclass
21) Robertiscreatinga java applicationIwhichcustomerdetailsneedtobe displayedfromthe
customerdatabase inSQL.Whichinterface shouldhe use toestablishconnectionbetweenthe SQL
database and the Javaapplication.
a) StatementInterface
b) ConnectionInterface
c) ResultsetInterface
7. Java 7 NIIT
Page 7 of 7
d) DriverInteface
22) What doesa DriverInterface in a JDBC programindicates
a) Loadsdriverfor a DB
b) RepresentsaDB server
c) ExecutesSQLStatement
d) RepresentsinformationretrievedfromaDB