Enable contrast version

# Tutor profile: Stephen P.

Inactive
Stephen P.
High school computer science teacher
Tutor Satisfaction Guarantee

## Questions

### Subject:PSAT

TutorMe
Question:

Without using a calculator, which of the following numbers are divisible by 6? $(I:26,172 \\II: 42,346\\III: 7,776\\IV: 1,679,616$) A) $$I$$ only B) $$I$$ and $$II$$ only C) $$III$$ and $$IV$$ only D) Both $$A$$ and $$C$$

Inactive
Stephen P.

Answer: D, Both $$A$$ and $$C$$. The obvious way to this solution is spending 4 minutes long-dividing by hand, but chances are, this wouldn't be the most (or only) time-consuming question you'll face in the no-calculator math section. With problems like this, it helps to remember some divisibility rules. For starters: $$\rightarrow\textbf{A number is divisible by 6 if and only if it is divisible by both 2 and 3.}$$ That's a good start. You probably also know that $$\rightarrow\textbf{A number is divisible by 2 if its last digit is even.}$$ That's less of a revelation, and it seems that every number here is divisible by 2. But what about being divisible by 3? $$\rightarrow\textbf{A number is divisible by 3 if the sum of its digits is divisible by 3.}$$ It'll take some addition, but that's faster than long division. Let's give it a shot. We'll start with $$I$$, since it will eliminate the most options if it's not divisible by 6: $$I: (2+6)+(7+1)+2=(8+8)+2=16+2=18$$ We know that 18 is divisible by 3 ($$3\times6=18$$), so option $$I$$ is divisible by 6. Knowing this, we can take another stab at eliminating some answers. We can try to eliminate either $$III$$ or $$IV$$, since every other answer already implies the divisibility of $$I$$ by 6. $$III$$ will obviously be easier to crack: $$III: (7+7+7)+6=21+6=27$$ Now, we know that 27 is divisible by 3 ($$3\times9=27$$), but suppose we didn't. As it turns out, this test for divisibility by 3 can be applied $$over\ and\ over$$: $$2+7=9$$ which is indeed divisible by 3, so $$III$$ must also be divisible by 6. Because there's no choice for "None of the above" (which would imply the possibility of $$II$$ being divisible by 6, but not $$IV$$), we can deduce that $$IV$$ is also divisible by 6 just from the answers, so the answer must be D, Both $$A$$ and $$C$$. FUN FACTS: $$III$$ is $$6^{5}$$, $$IV$$ is $$6^{8}$$, and $$I$$ is, coincidentally, $$6\times4,362$$.

### Subject:Java Programming

TutorMe
Question:

Why does Java forbid referring to non-static variables from static contexts?

Inactive
Stephen P.

The short answer is: ambiguity. Where non-static (or, "instance") variables or methods apply to a single object at a time (the object they belong to), static variables and methods can be thought of as applying to $$every$$ instance of a class at once. Because of this, referring to an instance (non-static) variable within a static method is kind of like calling a tire manufacturer and asking what the tire pressure is (using the $$\texttt{getTirePressure()}$$ method): $$\texttt{public class Tire}\{$$ $$\texttt{private float pressure;}$$ $$\texttt{public Tire(float p)}\{$$ $$\texttt{pressure=p;}$$ $$\}$$ $$\texttt{static float getTirePressure()}\{$$ $$\texttt{return pressure;}$$ $$\}$$ $$\}$$ That factory might make many different tires for many different operating pressures (not to mention that not every tire leaves the factory pressurized), so the question, "What's the tire pressure?" (an "instance" question, if you will) in that context - a $$static$$ context - is a hard kind question to answer, especially for a computer. You could be asking about $$any$$ tire that company has ever made, wherever it is and whatever it's currently being used for - it's simply too ambiguous. In order to avoid this conundrum, the compiler simply disallows any possibility of it, and forbids this kind of direct reference to non-static variables inside static contexts. $$(Try\ compiling\ the\ example,\ and\ notice\ the\ error\ that\ occurs)$$

### Subject:C++ Programming

TutorMe
Question:

When writing a C++ class, when should you include a destructor function?

Inactive
Stephen P.

A handy tip for answering this question is this: Only $$\texttt{delete}$$ when you have had to allocate (usually with $$\texttt{new}$$ in C++). With that in mind, a destructor function should be included in any class that defines an object that *allocates* memory. For example: $$\texttt{class Foo}\{\\$$ $$\>\>\texttt{private: }\\\>\>\>\> \texttt{int a;}\\$$ $$\>\>\texttt{public:}\\\>\>\>\> \texttt{Foo()}\{\\\>\>\>\>\>\> \texttt{a=5;}\\\>\>\>\> \};$$ $$\}$$ Does not need a destructor function, since none of the memory in a $$\texttt{Foo}$$ object will be explicitly allocated by this class (e.g., with $$\texttt{new}$$), and everything included in a $$\texttt{Foo}$$ object will be properly disposed when a calling program exits. On the other hand: $$\texttt{class Foo}\{\\$$ $$\>\>\texttt{private: }\\\>\>\>\> \texttt{int* a;}\\$$ $$\>\>\texttt{public:}\\\>\>\>\> \texttt{Foo()}\{\\\>\>\>\>\>\> \texttt{a=new int(5);} \\\>\>\>\> \};$$ $$\}$$ Should include a destructor function, since $$\texttt{int* a}$$ is allocated with $$\texttt{new int()}$$ in Foo's constructor: $$\texttt{class Foo}\{\\$$ $$\>\>\texttt{private: }\\\>\>\>\> \texttt{int* a;}\\$$ $$\>\>\texttt{public:}\\\>\>\>\> \texttt{Foo()}\{\\\>\>\>\>\>\> \texttt{a=new int(5);} \\\>\>\>\> \};$$ $$\>\>\>\> \texttt{~Foo()}\{\\\>\>\>\>\>\> \texttt{delete a;} \\\>\>\>\> \};$$ $$\}$$ This ensures that when a program using $$\texttt{Foo}$$ objects exits, it doesn't just throw away the address of the allocated memory ($$\texttt{*a}$$ within the $$\texttt{Foo}$$ object itself), it also makes sure to destroy the information "living" at that address, according to the destructor we've defined.

## Contact tutor

Send a message explaining your
needs and Stephen will reply soon.
Contact Stephen