Oracle Interview Question for SDE-2s


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Set some B's variable in A's constructor. For this we need to be sure that instance of B is already created before creating instance of A.
This is kind of sharing a variable within classes. More generic implementation will be like Observer pattern.

class B
{
	public:
	bool isACreated;
	B() 
	{  
		isACreated = false;
	}
	bool FnIsACreated(){ return isACreated;}
};

class A
{
	A(const B & objB)
	{
		objB.isACreated = true;
	}
};

This is very simple and basic implementation.

- ash.taunk3 June 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think language provides any such api.
We need to devise our own mechanism.

On the lines of Ash above, I will rather prefer for Class B to have a static bool isAexist.
and this boolean wil be set class A's constructor and reset in class A's destructor.

- Varun June 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There isn't a reasonable way to find out whether or not an instance of a particular class already exists.

If you need to know this information, create a static boolean field and set it from the constructor:

class MyClass {
private static bool instanceExists = false;
public MyClass() {
MyClass.instanceExists = true;
}
}

- NEERAJGARG0710 June 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A Static integer member , incremented each time a constructor is called ?

public class MyClass {
        private static int count = 0;

        public MyClass() {
                count++ ;
        }

        int getCount()
        {
                return count;
        }

        public static void main(String [ ] args)
        {
                System.out.println("TEST") ;
                MyClass j1 = new MyClass() ;
                MyClass j2 = new MyClass() ;
                MyClass j3 = new MyClass() ;


                System.out.println("COunt ==  " + j3.getCount()) ;
        }
}

- Surej June 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A good solution !

- spiderman August 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.java.basic;

class A {

private static int _INSTANCE_COUNT = 0;

public A() {
_INSTANCE_COUNT++;
}

@Override
public void finalize() {
--_INSTANCE_COUNT;
}

public static boolean isCreated() {
return (_INSTANCE_COUNT > 0);
}

}

public class B {

public static boolean isACreated() {
return A.isCreated();
}

public static void main(String[] args) {
A a = new A();
System.out.println(isACreated());
}
}

- VIKASH September 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.java.basic;

class A {

private static int _INSTANCE_COUNT = 0;

public A() {
_INSTANCE_COUNT++;
}

@Override
public void finalize() {
--_INSTANCE_COUNT;
}

public static boolean isCreated() {
return (_INSTANCE_COUNT > 0);
}

}

public class B {

public static boolean isACreated() {
return A.isCreated();
}

public static void main(String[] args) {
A a = new A();
System.out.println(isACreated());
}
}

- VIKASH September 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can think off couple of approaches:
1. Use a static variable in class A to keep counter of the instances created and read it before creating The instance of B.
2. Use Mediator pattern where A sends a communication to the mediator when it's instance is created. B checks with the mediator if the instance of A is created. Otherwise, the mediator can notify B of the A's instance creation.
This concept can be extended to use queues/topics as in messaging (JMS).

- DDM July 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class A {

}
class B {
private boolean isInstanceOf;
B(Object a) {
if(a instanceof A) {
this.isInstanceOf = true;
} else {
this.isInstanceOf = false;
}
}

public boolean getIfInstanceOf() {
return isInstanceOf;
}
}
class CheckInstance {
public static void main(String[] args) {
A a = new A();
B b = new B(a);
B b1 = new B(b);
System.out.println(b.getIfInstanceOf());
System.out.println(b1.getIfInstanceOf());
}
}

- Nishit May 26, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More