0% found this document useful (0 votes)
18 views3 pages

Jubilant Questions

work

Uploaded by

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

Jubilant Questions

work

Uploaded by

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

Bandhan Nawal

Search in a row wise and columns wise sorted matrix


i/p-
10 20 30
11 25 80
35 50 90
40 60 100
55 85 120

x = 35

o/p- Yes

// n = 5, m = 3
boolean findX(int[][] arr, int n, int m, int x) {
//x=35;boolean found=false; int a=n-1; int b=m-1
for(int i=a;i >=0;i--){
int counter=b;
while(counter>=0){
if(x>arr[i][b])
b=counter;
break;
if(x==arr[i][b]){
found=true;
break;
}
if(counter==0 && x!=arr[i][b]){
b=counter;
}
counter--;
}

if(found){
return true;
}
}

return false;
}

Find nth node from the end of the Linked List


i/p: 1, 3, 4, 10, 5, 15, 20; n = 3
o/p: 5

class Node {
int data;
Node next;
}

int nthFromEnd(Node head) {

Node current=head;
int counter=0;
while(current!=null){
counter++;
current=current.next;
}
counter=counter-n;
counter++;
int count=0;
Node newOne=head;
while(count<counter){
newOne=newOne.next;
count++;
}
return newOne.value;

Error / Output:
class Test {
int var=80;
public static void method1() {
System.out.println("I am first");
}

public static void method1(int val) {


System.out.println("I am second with a value: " + val);
}

public void method2(String str) {


System.out.println("I am printing method2 input: " + str);
}

class TestExtension extends Test {


int var=90;

public static void method1() {


System.out.println("I am third");
}

public void method2(String str) {


System.out.println("Method2 of extension class: " + str);
}

public static void main(String[] args) {


TestExtension obj1 = new TestExtension();
Test obj2 = new TestExtension();
obj1.method1(); // I am third
obj1.method2("Hello"); // Method2 of extension class: Hello
obj2.method1(); // I am third
obj2.method2("Hello2"); // Method2 of extension class: Hello2
TestExtension.method1(4); // I am second with a value: 4
}
}

Error / Output:

class Value {
int x;
Value(int x) {
this.x = x;
}
}

class Test {

public static void main(String[] args) {


Value val1 = new Value(10);
Value val2 = new Value(20);
swap(val1, val2);
System.out.println("val1="+val1+" and val2="+val2);
System.out.println("i = " + val1.x + ", j = " + val2.x);
}

private static void swap(Value v1, Value v2) {


System.out.println("v1="+v1+" and v2"+v2);
Value temp = v1;
v1 = v2;
v2 = temp;
System.out.println("v1="+v1+" and v2"+v2);
}
}

OUTPUT:
v1=Value@2a139a55 and v2Value@15db9742
v1=Value@15db9742 and v2Value@2a139a55
val1=Value@2a139a55 and val2=Value@15db9742
i = 10, j = 20

You might also like