When does an object get destroyed in programming?
I agree with @Hilmar. You can never predict when exactly the object will be destroyed. But, you can definitely predict when an object is ready to be destroyed.
Difference between the two can be easily explained using an example (I'm using JAVA here):
Employee emp = new Employee("Dexter");
This statement creates an object of Employee (with name Dexter) which resides in the heap.
As long as you are using the reference "emp" for this object , the object will stay there. Its indestructible. Garbage Collector cannot even touch it.
Now lets say, after a few lines of code, you are executing:
emp = new Employee("RITA");
This creates a new object of Employee (with name RITA), but notice that the reference "emp" no longer points to our old object with the name Dexter. Hence the object of Employee with the name Dexter has no references associated with it.
And this makes it eligible for the Garbage Collection, and whenever the next round of Garbage Collection happens, the mighty GarbageCollector will not leave this chance to eat up the Employee (with the name Dexter).
tough luck
What do you mean by procedure?
A procedure is a specified series of actions or operations which have to be executed in the same manner in order to always obtain the same result under the same circumstances (for example, emergency procedures). Less precisely speaking, this word can indicate a sequence of activities, tasks, steps, decisions, calculations and processes, that when undertaken in the sequence laid down produces the described result, product or outcome. A procedure usually induces a change. It is in the scientific method. So it is in Particular!
How many unique values can you assign in a 3 byte alphanumeric field?
There are 26 alphabetic characters (a-z) and 10 numeric (0-9) which together form 36 alphanumeric characters. If you include capital letters, then you have 62 (36 + 26) alphanumeric characters.
62 * 62 * 62 = 238,328
What method that returns string representation of the contents of the variable?
Since the question is in the Java category: in Java, the method is called toString(). This method will automatically be invoked if you implicitly convert an object to String type, for example:
"The answer is: " + myObject
In this example, the String concatenation (the plus sign) forces the object, myObject, to type String - to do this, the object's toString() method will be called.
#include
#include
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
string myNumber = "";
cout << endl << "Enter a number: ";
cin >> myNumber;
cout << endl << "You number was saved as string: "" << myNumber << """ << endl;
system("PAUSE");
return 0;
}
What does this mean import com.otherwise.jurtle.?
The "import" statement in Java imports names from some other package into the current context. So, if there was a class called com.otherwise.jurtle.SomeClass, you would have to refer to it by the full name, unless you imported it.
The import can be done specifically for one class:
import com.otherwise.jurtle.SomeClass;
or for everything in a package:
import com.otherwise.jurtle.*;
In Java 5 and up, you can also import all the static functions from a class:
import static com.otherwise.jurtle.SomeClass.*;
The "com.otherwise.jurtle" part is called the package identifier. The general practice is for a company to reverse its domain name for this. So jurtle.otherwise.com becomes com.otherwise.jurtle.
How can the base class data be accessed from database directly using derived class?
In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.
public class BasicThreads {
public static void main(String[] args) {
Thread odd = new Thread (new NumberPrinter());
Thread even = new Thread(new NumberPrinter());
odd.start();
even.start();
}
}
class NumberPrinter implements Runnable {
private static boolean printEven;
@Override
public void run() {
int start = printEven ? 2 : 1;
String type = printEven ? "Even Printer " : "Odd Printer ";
printEven = !printEven;
for ( ; start < 20 ; start += 2) {
System.out.println(type + start);
try {
Thread.sleep(100);
}
catch (Exception ex){
}
}
}
}
Is run time initialization of an array an initialization or an assignment?
It is an initialisation. You cannot assign values to a dynamic array until it has been initialised. Static arrays can be initialised at compile time. They can also be assigned at compile time.
void main()
{
// Compile time:
// =========
// Initialise a static array.
int arr1[10];
// Initialise and assign a static array.
int arr2[10] = {0,1,2,3,4,5,6,7,8,9};
// Runtime:
// ======
// Dynamic array (size unknown, no memory allocated)
int* pArr[];
// Initialise (size known, allocate memory, 40 bytes):
pArr = ( int* ) malloc( 10 * sizeof( int ));
// Assign (set values of elements):
int x;
for(x=0;x<10;++x)
pArr[x] = x;
// Uninitialise (release memory).
free( pArr );
return( 0 );
}
Write a program to print a right angle triangle containing digit in java?
let s1,s2,s3 be three sides of a triangle.
import java.lang.*;
import java.io.*;
import java.util.*;
class Triangle
{
public static void main(String args[])
{
boolean test=false;
int s1,s2,s3;
Scanner input = new Scanner(System.in);
System.out.println("enter the side1 of triangle");
s1=input.nextInt();
System.out.println("enter the side2 of triangle");
s2=input.nextInt();
System.out.println("enter the side3 of triangle");
s3=input.nextInt();
if((s1*s1)==(s2*s2)+(s3*s3))
{
test=true;
}
else if((s2*s2)==(s1*s1)+(s3*s3))
{
test=true;
}
else if((s3*s3)==(s1*s1)+(s2*s2))
{
test=true;
}
if(test==true)
System.out.println("Entered sides form a right angle triangle.....");
else
System.out.println("Entered sides dosn't form a right angle triangle.....");
}
}
What is an exception With an example program explain how it is handled?
An exception is literally that: an exception. Exceptions are thrown when something that shouldn't happen does. For example, if you're trying to turn "Hi" into an Integer, you'll get a NumberFormatException (in Java.) If there is nothing in place to handle an exception, the program will crash. Try-catch blocks are used for exception handling within methods, and exception throwing is used to give out exceptions, or let a different method handle it.
Example program (Java):
class ex{
public static void main(String[] args){
String Strx = "5a";
int x = 0;
try{
x += Integer.parseInt(Strx);
}catch(NumberFormatException e){ // If Strx is not an Integer
x += Integer.parseInt("5");
}
System.out.println("The value of x is " + x); // The value of x is 5
}
}
Alternatively, you could remove the try-catch blocks, and simply write "public static void main(String[] args) throws NumberFormatException" in the 2nd line of the program.
What is non-instantiable class?
A non-instantiable class is the class whose object can be created but cannot be initialized.
for example the interfaces and the abstract classes in java.
Write a Program for Array implementation of queue and stack using exception handling.?
include <iostream>
using namespace std;
#define SIZE 10
template <class StackType> class stack {
StackType stck[SIZE];
int topOfStack;
public:
void init() {
topOfStack = 0;
}
void push(StackType ch);
StackType pop();
};
template <class StackType>
void stack<StackType>::push(StackType ob)
{
try {
if(topOfStack==SIZE) throw SIZE;
} catch(int) {
cout << "Stack is full.\n";
return;
}
stck[topOfStack] = ob;
topOfStack++;
}
template <class StackType>
StackType stack<StackType>::pop()
{
try {
if( topOfStack == 0)
throw 0;
} catch(int) {
cout << "Stack is empty.\n";
return 0;
}
topOfStack--;
return stck[topOfStack];
}
int main()
{
stack<char> stack1, stack2;
int i;
stack1.init();
stack2.init();
stack1.push('a');
stack2.push('x');
stack1.push('b');
stack2.push('y');
stack1.push('c');
stack2.push('z');
for(i = 0; i <3; i++)
cout << "Pop stack1: " << stack1.pop() << endl;
for(i = 0; i <4; i++)
cout << "Pop stack2: " << stack2.pop() << endl;
// demonstrate double stacks
stack<double> doubleValueStack1, doubleValueStack2; // create two stacks
// initialize the stacks
doubleValueStack1.init();
doubleValueStack2.init();
doubleValueStack1.push(1.1);
doubleValueStack2.push(2.2);
doubleValueStack1.push(3.3);
doubleValueStack2.push(4.4);
doubleValueStack1.push(5.5);
doubleValueStack2.push(6.6);
for(i = 0; i <3; i++)
cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl;
for(i = 0; i <4; i++)
cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl;
return 0;
}
What is the difference between sand replacement method and core cutter method to find out density?
Core cutter method is simple but results approximate and used on soft soils
Imagine that we both work for the same project and first you wrote the code for a class, and then I used your class in my program. Later on, you didn't like the way the class behaved, because some of its instance variables were being set (by me from my code) to values you hadn't anticipated. Their code brought out errors in your code. (Relax, I wont do that, dont worry.) Since, it is a Java program, so you should be able just to ship out a newer version of the class, which I could replace in my programs without changing any of my own code.
The above scenario highlights two of the promises or rather i should say benefits of Object Orientation (OO): flexibility and maintainability. But these benefits will not come automatically. You have to do something. You have to write your classes and code in a way that supports flexibility and maintainability. Just because Java supports OO concepts, it cannot write code for you. Can it?? For example, imagine if you made your class with public instance variables, and those other programmers were setting the instance variables directly, as the following code demonstrates:
public class BadExample {
public int size;
public int weight;
...
}
public class AnotherBadExample {
public static void main (String [] args) {
BadExample b = new BadExample ();
b.size = -5; // Legal but bad!!
}
}
Now go back the scenario we spoke about a paragraph ago. BadExample is your class and AnotherBadExample is my code. I have modified one of your variables in a way that it helps my code logic but that totally alters the way your class works. Now you are in trouble. How are you going to change your class in such a way that no one can alter your values directly (like what i have done in my code)? Your only choice is to write a method say setSize(int newVal) inside your class and then change the access modifier of the variable size to say, private. This will ensure that you handle instances when someone is trying to set a value to the size variable that you dont want and at the same time ensure that no one can access the size variable directly and mess with your code.
But, unfortunately, by doing that, you have broken my code. If I try to compile my AnotherBadExample class, i will get errors because the size variable is no longer visible for me.
How can we address this situation now? The best way is: not write such code where public variables are available for anyone and everyone to modify.
The ability to make changes in your code without breaking the code of all others who use your code is a key benefit of encapsulation. You should always hide implementation details. To elaborate, you must always have your variables as private and then have a set of public methods that others can use to access your variables. Since the methods are public anyone can access them, but since they are in your class you can ensure that the code works the way that is best for you. So in a situation that you want to alter your code, all you have to do is modify your methods. No one gets hurt because i am just using your method names in my code and the code inside your method doesnt bother me much.
If you want maintainability, flexibility, and extensibility (and I guess, you do), your design must include encapsulation. How do you do that?
Why do you declare a string as char asterisk?
Declaring strings as char* is generally faster than declaring an array of type char.
Consider the following:
#include<stdio.h>
int main (void) {
char c1[12] = "Hello world";
char c2[] = "Hello world";
char* c3 = "Hello world";
// ...
return 0;
}
The string literal, "Hello world" is duplicated three times in the source code but in the resultant machine code that string only appears once (in the data segment). This is known as string-pooling; it makes no sense to maintain separate copies of the exact same value in the data segment.
Given that there is only one copy of the string, it would be natural to assume that c1, c2 and c3 all refer to the same memory location but they don't. In fact, the only one that does refer to the data segment copy is the third one.
If we examine the assembly for the c1 initialisation we find the following:
mov eax, DWORD PTR ??_C@_0M@KIBDPGDE@Hello?5world?$AA@
mov DWORD PTR _c1$[ebp], eax
mov ecx, DWORD PTR ??_C@_0M@KIBDPGDE@Hello?5world?$AA@+4
mov DWORD PTR _c1$[ebp+4], ecx
mov edx, DWORD PTR ??_C@_0M@KIBDPGDE@Hello?5world?$AA@+8
mov DWORD PTR _c1$[ebp+8], edx
The symbol ??_C@_0M@KIBDPGDE@Hello?5world?$AA@ identifies the string in the data segment but the assembly clearly shows data is being copied (moved) to a new location 32-bits (4 bytes) at a time. The new location is the address of c1. The same thing happens for c2, making yet another copy of the string.
However the c3 initialisation consists of just one instruction.
mov DWORD PTR _c3$[ebp], OFFSET ??_C@_0M@KIBDPGDE@Hello?5world?$AA@
The difference is that instead of copying the string to new memory, we simply copy the address of the string.
This is low-level stuff of course, but it makes no sense to copy objects from the data segment or indeed anywhere else when we can simply refer to them directly using a pointer. We only need to copy objects when we actually intend to make some change to the object without affecting the original object. In all other cases we can simply refer to them.
How do you use Necromancer class?
If that's for some game, please clarify what game you are talking about - or make sure the question gets in the correct category of questions.