0% found this document useful (0 votes)
5 views

oop Answer

The document contains various Java code snippets demonstrating different programming concepts, including string manipulation, exception handling, threading, and class inheritance. It also includes outputs and explanations for the code, highlighting key behaviors and outcomes. Additionally, it discusses abstract classes and the Command Pattern in programming.
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)
5 views

oop Answer

The document contains various Java code snippets demonstrating different programming concepts, including string manipulation, exception handling, threading, and class inheritance. It also includes outputs and explanations for the code, highlighting key behaviors and outcomes. Additionally, it discusses abstract classes and the Command Pattern in programming.
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/ 13

class test {

public static void main(String args[]) {

String s = "Hellow";
String t = new String(s);

if ("Hellow".equals(s)) {
System.out.print("one");
}
if (t == s) {
System.out.print("two");
}
if (t.equals(s)) {
System.out.print("three");
}

}
}
class Cat {

public String name;

public void parseName() {


System.out.print("1");
try {
System.out.print("2");
int x = Integer.parseInt(name);
System.out.print("3");
} catch (NullPointerException e) {
System.out.print("5");
}
System.out.println("5");
}

public static void main(String args[]) {

Cat felix = new Cat();


felix.name = "Felix";
felix.parseName();
System.out.print("6");
}
}
12Exception in thread "main" java.lang.NumberFormatException:
public class test {

public static void main(String args[]) {


StringBuilder sb = new StringBuilder("I love java");
sb.substring(0, 9);
sb = sb.reverse();
System.out.println(sb);
}
}

public class test {


public static void main(String args[]) {
int x = 5, y = 10;
boolean b = x < 0;

if (b = true) {
System.out.print(x);
} else {
System.out.print(y);
}
}
}
public abstract class Writer{
public void method1();
public final void method2(){};
public static void method3(){};
public abstract static void method4(){};
public abstract final void method5();
}
public class Example {

static int a = 10;

public static void main(String args[]) {


Example s1 = new Example();
System.out.println("s1.a value : " + s1.a);
s1.a = 20;
System.out.println("s1.a value : " + s1.a);

}
}

//output
s1.a value : 10
s1.a value : 20

Answer : 1020
public class PrintA extends Thread {
public void run() {
System.out.print("A");
}
}

public class PrintB {

public static void main(String args[]) {


Thread a = new PrintA();
a.start();
System.out.print("B");

}
}

The output is always BA

Answer – The output is always AB


public class Ex01 {

static int no = 10;

static {
no = no + add(no);
System.out.println(no);
}

static int add(int no) {


return no;
}

{
System.out.println(no);
}

public Ex01() {
System.out.println("Constructor");
}

public static void main(String args[]) {


test ex01 = new Ex01();
}
}

20
20
Constructor

Answer is 20
Answer is Command Pattern
Answer – classes, interface, static, implicitly
abstract class Writer {

abstract void write();

void read() {
System.out.println("Reading...");
}

public class programmer extends Writer {


programmer() {
super();
}

void write() {
System.out.println("writing..");

}
}
Answer – A,D,E

You might also like