Java Programming Fundamentals
Covers Java basics including arrays, loops, constructors, inheritance, and method overloading
Questions
What is the output of the given program?
public class ScopeTest
{
public static void main (String [] args)
{
int[][] xx[][][] = null;
System.out.println(xx);
}
}
- Compilation Fails
- Null
- 0
- Java.lang.ArrayIndexOutOfBoundException
- Java.lang.NullPointerException
Which of the following packages are automatically imported into the java source file by the java compiler?
Public Class Test
{
}
- Java.lang
- Java.awt
- The package with no name
- Java.*
- Both (1) and (3)
What is the output of the given program?
public class ScopeTest
{
public static void main (String [] args)
{
int ii = 2;
do
{
System.out.println (ii);
}
while (--ii);
}
}
- 1
- 2
- Null
- Infinite loop
- Compilation Error
Which of the following is the output of the given program?
public static void main(Stirng[] args)
{
int x = 353;
int j = x++;
switch (j)
{
case 317: case 353: case 367: System.out.println(Is a prime number.);
case 353: case 363: System.out.println(Is a palindrome.);
break;
default: System.out.println(Invalid value.);
}
}
- It is a prime number.
- It is a palindrome.
- It is a prime number. It is a palindrome.
- Invalid value
- Compilation fails
What is the output of the given program code?
public class ScopeTest
{
int z;
public static void main(String[] args)
{
ScopeTest myScope = new ScopeTest();
int z = 6;
System.out.println(myScope.z);
System.out.println(z);
myScope.doStuff();
System.out.println(z);
System.out.println(myScope.z);
}
void doStuff()
{
int z = 5;
doStuff2();
System.out.println(z);
}
void doStuff2()
{
z = 4;
}
}
- Garbage value 6 5 6 4
- 6 6 5 6 4
- 0 6 5 6 4
- Compilation error
- Run time error
What is the output of the given program?
public class ScopeTest
{
public static void main (String [] args)
{
int[][] xx[][][] = null;
if(xx)
{
System.out.println(xx);
}
else
{
System.out.println("False");
}
}
}
- Null
- False
- Java.Lang.NullPointerException
- 0
- Compilation fails
Which of the given classes has default constructor?
Class X { }
Class Y { Y() {} }
Class Z { Z(int i) { } }
- X Only
- Y Only
- Z Only
- X and Z
- X and Y
Which of the following are valid declarations for two dimensional array?
- Int[][] Array;
- Int[] Array[];
- Int[2][2] Array;
- Both (1) and (2)
- All of these
What is the output of the given program code?
public class ScopeTest
{
public static void main(String[] args)
{
int [] xx = null;
for(int i=0;i
- Null
- Java.lang.NullPointerException
- 0
- Compilation fails
- None of these
How many times is “aa” printed as a part of output?
public class ScopeTest
{
int z;
public static void main(String[] args)
{
String[] table = {"aa", "bb", "cc"};
for (String ss: table)
{
int ii = 0;
while (ii < table.length)
{
System.out.println(ss + ", " + ii);
ii++;
}
}
}
}
- Zero times
- Once
- Twice
- Thrice
- Compilation fails
What is the output of the given program?
class ScopeTest
{
int x(double d)
{
System.out.println("one");
return 0;
}
String x(double d)
{
System.out.println("two");
return null;
}
double x(double d)
{
System.out.println("three");
return 0.0;
}
public static void main(String[] args)
{
new Overloading().x(4.0);
}
}
- One
- Two
- Three
- Compilation fails
- Run time error
What is the output of the given program code?
public class ScopeTest
{
public static void main(String[] args)
{
ScopeTest sc, scA, scB;
sc = new ScopeTest();
scA = new ScopeTestA();
scB = new ScopeTestB();
System.out.println("Hash is : " + sc.getHash() + ", " + scA.getHash() + ", " + scB.getHash());
}
public int getHash()
{
return 111111;
}
}
class ScopeTestA extends ScopeTest
{
public long getHash()
{
return 44444444;
}
}
class ScopeTestB extends ScopeTest
{
public long getHash()
{
return 999999999;
}
}
- Hash is: 111111, 44444444, 999999999
- Blank output screen
- An exception is thrown at runtime
- Compilation Fails
- None of these
What is the output of the given program?
Int [] [] array = {{0}, {0, 1}, {0, 2, 4}, {0, 3, 6, 9}, {0, 4, 8, 12, 16}};
Systemout.printIn(array [2] [1]);
System.out.printIn (array) [2][4]);
- 1 null
- Null null
- 2 an ArrayIndexOutofbound Exception is thrown
- ArrayIndexOutofbound Exception is thrown
- Compilation fails
What is the output of the given program?
class A
{
int a;
A(int a)
{
System.out.println(a);
}
public void display()
{
System.out.println("Hello");
}
}
class ScopeTest extends A
{
public static void main(String[] args)
{
ScopeTest s1 = new ScopeTest();
s1.display();
}
}
- Hello
- 0
- Run time error
- Compilation error
- Blank output screen
How many times is 1 printed as a part of output?
public class ScopeTest
{
int z;
public static void main(String[] args)
{
String[] table = {"aa", "bb", "cc"};
for (String ss: table)
{
int ii = 0;
while (ii < table.length)
{
System.out.println(ss + ", " + ii); ii++;
}
}
}
}
- Zero times
- Once
- Twice
- Thrice
- Compilation fails