0% found this document useful (0 votes)
26 views8 pages

MCQs

The document contains examples of Java code demonstrating inheritance, threads, string operations, arrays and loops. It provides the output of running each code example.

Uploaded by

Syed Ibad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views8 pages

MCQs

The document contains examples of Java code demonstrating inheritance, threads, string operations, arrays and loops. It provides the output of running each code example.

Uploaded by

Syed Ibad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

public class Parent {

static final String str = "I am parent class variable";


static String str2 = "I am child class variable";

public static void childMethod() {


int var = 20;
System.out.println(str);
System.out.println("Variable integer value is "+var);
System.out.println(str2);
}
public static void main(String[] args) {
Parent.childMethod();
}
}

ANS:

I am parent class variable


Variable integer value is 20
I am child class variable

=============================================================================

public class switchMember {


void switchNumber(long number) {
switch(number) {
case 1.0:
System.out.println("floating point");
case 1:
System.out.println("Integer ");
}
}

public static void main(String[] args) {


// TODO Auto-generated method stub
switchMember s = new switchMember();
s.switchNumber(1l);
}

ANS:
Cannot switch on a value of type long. Only convertible int values, strings
or enum variables are permitted

=============================================================================

public class Test1 {


static boolean b1;
static Boolean b2;
static boolean[] a1 = new boolean[1];
static Boolean[] a2 = new Boolean[1];
public static void main(String[] args) {
Boolean check;
if(b1||b2)
check = a1[0] = a2[0];
else if (b1&&b2)
check = a2[0] = a1[0];
else
check = a1[0] == a2[0];
System.out.println(check);
}

ANS:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke


"java.lang.Boolean.booleanValue()" because "assessment.Test1.b2" is null
at assessment.Test1.main(Test1.java:10)

=============================================================================

import java.util.ArrayList;

import java.util.List;

class A{}

class B extends A{}

class C extends B{}

public class Test2 {

public static void main(String[] args) {

List<? super B> l1 = new ArrayList<>();

List<? extends B> l2 = new ArrayList<>();

ANS:

l1.add(new C());

=============================================================================

public class Test3 {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.println(MIN_VALUE);
}

ANS :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:


MIN_VALUE cannot be resolved to a variable

=============================================================================

public class Thread0 extends Thread{


public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread0 t2 = new Thread0();
t2.start();
}
}

ANS :

Thread-0

=============================================================================
public class ThreeLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 5;
int s = 0;
for(int i =0; i<n;i++) {
for(int j =i; j<n;j++) {
for(int k=j; k<n;k++) {
s++;
}
}
}
System.out.println(s);
}
}

ANS :

35

=============================================================================

public class TwoLoop {

public static void main(String[] args) {


// TODO Auto-generated method stub
int n = 10;
int sum = 0;
for(int i =0; i<n;i++) {
for(int j =i; j<n;j++) {
sum += i+j;
}
}
System.out.println(sum);
}

ANS :

495

=============================================================================
import java.util.regex.Pattern;

class ABC {

public static void main(String[] args) {


String text="Hello1how2are3you4";
String delimiter="\\d";
Pattern pattern=Pattern.compile(delimiter,Pattern.CASE_INSENSITIVE);
String[] result=pattern.split(text);
for(String temp:result)
System.out.println(temp);

ANS:

Hello
how
are
you

=============================================================================

public class EigthQuestion {

public static void main(String[] args) {


int n=5;
int[][] a=new int[n][n];
int count=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
a[i][j]=count++;
}
}
System.out.println(a[2][3] + a[4][1]);

}
Ans: 34
=============================================================================

public class FirstQuestion implements Runnable{

public static void main(String[] args) {


FirstQuestion obj = new FirstQuestion();
Thread t = new Thread(obj);
t.start();
System.out.println("Java");
try {
t.join();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Prog");

@Override
public void run() {
System.out.println("Welcome ");
System.out.println("To ");

Ans :
Java
Welcome
To
Prog

=============================================================================

public class FourthQuestion {

public static void main(String[] args) {


int x=5;
int y=7;
int z=3;
if(x>y) {
if(x>z) {
System.out.println("x");
}
else {
System.out.println("z");
}
}else {
if(y>z) {
System.out.println("z");
}
else {
System.out.println("y");
}
}

Ans: z

=============================================================================

class Master{

final public void show() {


System.out.println("Master::show() called");
}
}
class branch extends Master{

}
class Main {

public static void main(String[] args) {


Master b=new branch();
b.show();

Ans: Runs But Compile Error

=============================================================================

public class NinethQuestion extends Thread {

public void run() {


System.out.println(Thread.currentThread().getName());
}

public static void main(String[] args) {


NinethQuestion n1 = new NinethQuestion();
NinethQuestion n2 = new NinethQuestion();
NinethQuestion n3 = new NinethQuestion();

n1.start();
n2.start();
n3.start();
}

Ans: Thread-1

Thread-0
=============================================================================

public class SeventhQuestion {

static boolean b1;


static Boolean b2;
static boolean[] a1=new boolean[1];
static Boolean[] a2=new Boolean[1];
public static void main(String[] args) {
Boolean check;
if(b1 || b2)
check = a1[0]=a2[0];
else if(b1 && b2)
check = a2[0]=a1[0];
else
check = a1[0]==a2[0];
System.out.println(check);
}

Ans: Null Pointer exception or Run time Error

=============================================================================

public class SixthQuestion extends Thread{

public void run() {


System.out.println(Thread.currentThread().getName());
}

public static void main(String[] args) {


SixthQuestion s=new SixthQuestion();
s.start();

Ans: Thread-0

=============================================================================

public class STtringExample {


public static void main(String[] args) {
String str=new String("person");
String s1=str.concat("Boss");
System.out.println(s1);
String s2=str.concat(s1);
System.out.println(s2);
System.out.println(str);

Ans: personBoss

personpersonBoss
person

=============================================================================

class Faculty{

float salary=30000;
}
public class TenthQuestion extends Faculty {
float bonus=2000;
public static void main(String[] args) {
// var v1=null;// Line
// var v2=0,v3=1;// Line2
// var v4= 1L + 1F;// Line3
// Ans: Line3 only
TenthQuestion ts = new TenthQuestion();
System.out.println("Salary is: "+ts.salary);
System.out.println("Bonus is: "+ts.bonus);
//

Ans : Line3 only

You might also like