JAVA Programming :: Packages

Home > Technical Aptitude > JAVA Programming > Packages > General Questions

NA
SHSTTON
1
Solv. Corr.
6
Solv. In. Corr.
7
Attempted
0 M:0 S
Avg. Time

1 / 6

Choose the correct option.

Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class.


APublic

BProtected

CPrivate

DNone

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
3
Solv. Corr.
3
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

2 / 6

Choose the correct option.

Which of the following are legal identifier names in Jav Select the correct answers.


A%abcd, $abcd

B$abcd, _a_long_name

C1abcd, _a_long_name

Dpackage

Answer: Option B

Explanation:

Java identifier name cannot begin with a digit.

Workspace

NA
SHSTTON
2
Solv. Corr.
4
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

3 / 6

Choose the correct option.

You have a class file name Test.class inside javaproject directory.

Test.java source code is :
import javutil.Properties;
class Test {
public static void main (String[] args){
Properties p = System.getProperties();
System.out.println(p.getProperty("key1"));
}
}
you have issued below commands from command prompt.
cd javaproject
java -D key1=value1 Test
What is the output ?


Avalue1

Bnull

CRun successfully but no output

DRun fails - javlang.NoClassDefFoundError: key1=value1

Answer: Option D

Explanation:

name=value pair must follow immediately, no spaces allowed. In this case there is space between -D and key1=value1 So javlang.NoClassDefFoundError:
key1=value1.

Workspace

NA
SHSTTON
1
Solv. Corr.
4
Solv. In. Corr.
5
Attempted
0 M:0 S
Avg. Time

4 / 6

Choose the correct option.

HashMap can be synchronized by _______ ?


AMap m = Collections.synchronizeMap(hashMap);

BMap m = hashMap.synchronizeMap();

CMap m = Collection.synchronizeMap(hashMap);

DNone of the above

Answer: Option A

Explanation:

HashMap can be synchronized by Map m = Collections.synchronizeMap(hashMap);

Workspace

NA
SHSTTON
1
Solv. Corr.
4
Solv. In. Corr.
5
Attempted
0 M:0 S
Avg. Time

5 / 6

What is the output for the below code?
package bean;
public class Abc {
public static int index_val = 10;
}
package com;
import static bean.Abindex_val;
public class Test1 {
public static void main(String... args) {
System.out.println(index_val);
}
}

A10

Bcompile error, index_val not defined

CCompile error at import static bean.Abindex_val;

DNone of the above

Answer: Option A

Explanation:

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. J2SE 5.0 onwards it allows static import like import static bean.Abindex_val; and can be use directly System.out.println(index_val);

Workspace

NA
SHSTTON
2
Solv. Corr.
3
Solv. In. Corr.
5
Attempted
0 M:0 S
Avg. Time

6 / 6

What is the output for the below code?
public class D {
int i;
int j;
public D(int i,int j){
this.i=i;
this.j=j;
}
public void printName() {
System.out.println("Name-D");
}
}
public class Test{                                                                                                                                                            13. public static void main (String[] args){
D d = new D();
d.printName();
}
}

AName-D

BCompilation fails due to an error on lines 3

CCompilation fails due to an error on lines 4

DCompilation succeed but no output

Answer: Option B

Explanation:

Since there is already a constructor in this class (public D(int i,int j)), the compiler won't supply a default constructor. If you want a no-argument constructor to overload the with arguments version you already have, you have to define it by yourself. The constructor D() is undefined in class D. If you define explicit constructor then default constructor will not be available. You have to define explicitly like public D(){ } then the above code will work. If no constructor into your class , a default constructor will be automatically generated by the compiler.

Workspace