OOP Final
int num = 100;
public void calc(int num) {
this.num = num * 10;
}
public void printNum(){
System.out.print(
"The result of the calculation is : ");
System.out.println(num);
}
public static void main(String[] args) {
Calculator obj = new Calculator ();
obj.calc(2);
obj.printNum();
}
}
list.add("A");
list.add("B");
list.add("C");
list.add("D");
for (int I = 0; I < list.size(); i++)
System.out.print(list.remove(i));
public static void main(String[] args) {
int x = 2;
int y = 3;
System.out.println(x+y/x+y%x);
}
for (int x = 0; x < 3; x++) {
for (String s : sa) {
System.out.print(x + " " + s);
if (x == 1) {
break;
}
}
}
for (counter=1;counter<=3;++counter)
{
if (counter <= 1) {
System.out.print("UCF");
}
}
if (counter == 4)
System.out.print("UCF");
for (counter=1;counter<=3;++counter)
{
if (counter <= 1) {
System.out.print("UCF");
break;
}
}
if (counter == 4)
System.out.print("UCF");
String s2 = "abc";
String s3 = new String("abc");
System.out.print("A");
if (s1 == s2){
if (s1 != s3){
if (s2 == s3)
System.out.print("B");
}
else
System.out.print("C");
}
company = new Employee[3];
for (int i=0;i<3;++i){
company[i] = new Employee();
}
for (Employee e : company){
System.out.print(e);
}
class Employee{
public String toString() {
return "employee";
}
}
switch (score/10%10){
case 6: System.out.print (“UCF”);
case 7: System.out.print(“UCF”);
default: System.out.print(“UCF”);
}
if (score == 6)
System.out.print(“UCF”);
Integer i2 = new Integer(2);
Integer i3 = new Integer(3);
i1 = i2;
i2 = i3;
i3 = i1;
System.out.print(i1);
System.out.print(i2);
System.out.print(i3);
public static int a = 3;
public static int b = 2;
public static void setAll(int a, int b){
Test.a = a;
Test.b = b;
}
public Test(){
Test.setAll(2,3);
}
public void print() {
System.out.println("a = "+a+", b = "+b);
}
}
...
Test t = new Test();
t.print();
return ++a + b--;
}
...
int x = 2;
int y = 3;
y = doSomething(x,y);
System.out.println(x+","+y);
int I =0;
for (i=0;i<3;++i)
names[i] = new String("Abc");
names[i] = new String("Xyz");
for (i=0;i<3;++i)
if (i < 4)
System.out.print(names[i]);
private boolean flag = false;
public Test(boolean flag){
this.flag = flag;
}
public String toString() {
return “false”;
}
}
...
Test t = new Test(true);
System.out.println(t);
....
int calculate() {
return 1;
}
public static void main(String[] args){
System.out.println(calculate());
}
}
public static int x;
public static int y;
public Test()
{
x = 6;
y = 8;
}
public static void main(String[] args)
{
Test t = new Test();
System.out.println(Test.x+","+Test.y);
}
}
private int m_val;
private void method1 () {
method2();
m_val = 1;
}
public int getVal(){
return m_val;
}
public static void main(String args []) {
Question q = new Question ();
q.method1 ();
method2();
}
private static void method2() {
System.out.print(
(new Question ().getVal() +
" "+ new Question ());
}
public Question() { m_val = 5; }
public String toString() { return "6"; }
}
public static void main(String args[]) {
String myString = null;
try {
System.out.print(myString.length() + 5);
System.out.print("1");
} catch (NullPointerException npe) {
System.out.print("1");
} catch (Exception e) {
System.out.print("1");
} finally {
System.out.print("1");
}
}
}
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
c = a;
b = a;
System.out.println(b);
}
}
class A extends B {
public String toString() {
return "1";
}
}
class B extends C {
public String toString() {
return "2";
}
}
class C {
public String toString() {
return "3";
}
}
public static void main(String args[]) {
boolean goAgain = true;
while (goAgain == true) {
try {
int x = 5 / 0;
if (goAgain == false)
System.out.print(x);
goAgain = false;
System.out.print("1");
} catch (Exception e) {
goAgain = true;
System.out.print("2");
} finally {
System.out.print("3");
goAgain = false;
}
} // end of while
}
}
public static void main(String args[]) {
A ref = new B();
ref.data = 3;
int data = 4;
ref.show();
}
}
class A {
public int data;
public A() { data = 7; }
public void show() {
System.out.println(data);
}
}
class B extends A {
public void show() {
System.out.println(data);
}
}
public static void main(String args[]) {
A ref = new B();
ref.data = 3;
int data = 4;
ref.show(7);
}
}
class A {
public int data;
public void show() {
System.out.println(data);
}
}
class B extends A {
public void show(int data) {
System.out.println(data);
}
}
public String toString() {
return "9";
}
}
public class Main {
public static void main(String args[]) {
try {
int x = 1, y = 3;
System.out.print(y / x - x);
throw new DivisionByZero();
} catch (DivisionByZero dbz) {
System.out.print(dbz);
} catch (Exception e) {
System.out.print("1");
} finally {
System.out.print("1");
}
}
}
public static void main(String args[]) {
A ref = new B();
ref.data = 3;
int data = 4;
((B) ref).show(7);
}
}
abstract class A {
public int data;
public void show() {
System.out.println(data);
}
}
class B extends A {
public void show(int data) {
System.out.println(data);
}
}
public int doNothing() {
System.out.print("abc");
return 1;
}
}
public class Main extends MyAbstract {
public static void main(String args[]) {
MyAbstract a = new MyAbstract();
doNothing(a.doNothing());
}
public static void doNothing(int dummy) {
System.out.print("xyz");
}
}
public static void main(String args[]) {
String[] array = new String[2];
int I = 1;
try {
for (i = 0; I < 2; i++) {
array[i] = new String("9");
System.out.print(array[i-1]);
}
} catch (Exception e) {
System.out.print("!");
}
}
}
public static void main(String args[]) {
String[] array = new String[2];
int I = 1;
try {
for (i = 0; I < 2; i++)
array[i] = new String("9");
System.out.print(array[i-1]);
} catch (Exception e) {
System.out.print("!");
}
}
}
public static void main(String args[]) {
String[] array = new String[2];
String str;
int I = 1;
try {
for (i = 1; I < 3; i++)
array[i-1] = new String("9");
System.out.print(array[i-1]);
} catch (Exception e) {
System.out.print("!");
}
}
}
public static void main(String args[]) {
Test test1 = new Test() , test2 = new Test();
test1.data = 2;
test2.data = 1;
System.out.print( test1.compareTo(test2) + " " + test2.compareTo(test1));
}
}
class Test implements Comparable <Test>{
public int data;
public int compareTo( Test obj) {
if ( this.data < 2 + obj.data ) return 1;
return 0;
}
}
public static void main(String[] args) {
Manager manager = new Manager();
manager.salary = 33;
System.out.println(manager);
}
}
abstract class Person {
abstract public int returnCode();
}
interface Printable {
public void print();
}
abstract class Employee extends Person implements Printable {
public int salary;
public int returnCode() { return 30; }
}
class Manager extends Employee {
public void print(double salary) {
System.out.println(salary);
}
public String toString() {
return "COP" + salary + returnCode();
}
}
public static void main(String args[]) {
int[] array = {1, 2};
int elem;
try {
elem = 3;
throw new MyException();
} catch (MyException me) {
System.out.print(me);
} finally {
System.out.print("!!!");
}
}
}
class MyException {
public String toString() {
return "My Exception";
}
}
public int x;
public Parent() {
x = 1;
}
}
class Child extends Parent {
private int x;
public Child(int x) {
this.x = x;
x = super.x;
}
public void print(int x) {
System.out.print(super.x + " " + x + " " + this.x);
}
}
public class Main {
public static void main(String args[]) {
Parent pc = new Child(2);
((Child) pc).print(3);
}
}
public abstract void move(int miles);
public void printInfo() { System.out.print("Vehicle "); }
}
public class Car extends Vehicle {
private int distance;
public void move(int miles) {
distance = distance + miles;
}
public void printInfo() { System.out.print("Car "); }
public static void main(String args[]) {
Vehicle myVehicle = new Vehicle();
Car myCar = new Car();
myVehicle.printInfo();
myCar.printInfo();
}
}
public class Main {
public static ListNode method1(ListNode head) {
ListNode list = head;
while(list Is null) {
if (list.mNext == null) {
break;
}
if (list.mVal == list.mNext.mVal) {
list.mNext = list.mNext.mNext;
} else {
list = list.mNext;
}
return head;
}
}
What will be linked-list after calling method1 using the head=1,2,2,2,3,3,4,57
public class Main {
public static ListNode method1 (ListNode head, int val) {
ListNode tmp = head;
ListNode node = new ListNode (val);
if(head == null) {
return node;
}
while(tmp.mNext I= null) {
tmp = tmp.mNext;
}
tmp.mNext = node;
node.next = null:
return head;
}
}
What will be linked-list after calling method1 using the head=1,2,2,2,3,3,4,5 and val=2?
public class Main {
boolean method1 (Node head){
Node slows head;
boolean result = true;
Stack<Integer> stack = new Stack<integer>();
while (slow Is null) {
stack.push(slow.mVal):
slow = slow.mNext;
}
while (head I= null){
int I = stack.popt;
if (head.mVal l= i) {
result = false;
break;
}
head a head.mNext;
}
return result;
}
What will be linked-list after calling method1 using the head=1,2,2,2,3,3,4,57
public class Main {
List Node method1(ListNode head){
ListNode prev = null;
ListNode current = head;
List Node next = null;
while (current I= null) {
next = current.mNext;
current.mNext = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
}
What will be linked-list after calling method1 using the head=1,2,2,2,3,3,4,5?
public class Main{
static void method1(Node head)
{
ListNode temp = head;
if (head le null){
do {
System.out.print(temp.mVal +" =);
temp = temp.mNext;
} while (temp I= head);
}
}
What will be output after calling method1 using the circular linked list head 1,2,2,2,3,3,4,5?